This question already has answers here:
ActionBarActivity cannot resolve a symbol
(4 answers)
Closed 8 years ago.
I've been working through an Android Application tutorial on the developer android site for quite a while, but I can't get it to work perfectly for the life of me. My DisplayMessageActivity.java is filled with errors (i.e. getting undefined methods and unresolved types) Here are my files...
MainActivity.java
package com.miller.lab3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayMessageActivity.java
package com.miller.lab3;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Lab3</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">My Message</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
I'm wondering if anyone sees anything out of the ordinary. It would be greatly appreciated if you could point it out. I heard that this may or may not be an outdated version of the tutorial and the fact that I must use eclipse to do it doesn't exactly help. Thanks in advance.
If anyone doesn't know what I'm talking about here is the tutorial link http://developer.android.com/training/basics/firstapp/starting-activity.html
You code seems correct. Try cleaning the project. Go to Project>Clean>Clean selected project.If this doesn't work as happens in some cases, try creating a new project. However, if this is a big project, this method is useless. In such cases, there is a much better solution to this. Make use of git which is a version control system that keeps your project super safe and clean.
You are not importing ActionBarActivity
Add to your imports in DisplayMessageActivity.java:
import android.support.v7.app.ActionBarActivity;
or click on ActionBarActivity and press ctrl+1, and click Import 'ActionBarActivity'
Related
Following my previous post where I asked about implementing parceable objects, the solution I got was to use setRetainInstance(true); method.
#CommonsWare provided me a sample which I modified as follows:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="387dp"
android:layout_height="103dp"
android:text="New Text"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
RotationFragmentDemo.java
package com.rotationfragment;
import android.app.Activity;
import android.os.Bundle;
public class RotationFragmentDemo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This check returns 'null' for the first time when there are no fragments
if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
getFragmentManager().beginTransaction().add(android.R.id.content, new RotationFragment()).commit();
}
}
}
RotationFragment.java
package com.rotationfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RotationFragment extends Fragment implements
View.OnClickListener {
TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
// This should help to retain fragment
setRetainInstance(true);
View result=inflater.inflate(R.layout.activity_main, parent, false);
result.findViewById(R.id.button).setOnClickListener(this);
textView = (TextView)result.findViewById(R.id.textView);
return(result);
}
#Override
public void onClick(View v) {
textView.setText("Hello.");
}
}
Upon rotation the text in TextView is not restored.
I know some of you would suggest using parceable writeString parceable method to fix this simple example but I want to know why is the Fragment not retained here using setRetainInstance(true)?
And I don't need to retain a string but Socket, Thread and Activity as in previous post.
See the answer, "onRetainInstance saves the Fragment object, but I still have to rebuild UI in onCreateView", So you have to restore the state of your views manually
I have created fragments(6 fragment) in my app.In my first fragment(android_frag) there is a button "Show Places on Map". When I click on that button new activity names "ShowPlaces" opens up. In the show_places xml file I have created one button that should return me to the previous screen i.e the android fragment but button is not showing up in when the ShowPlaces activity opnes up, though I can see the button in graphical layout of the xml file. I have not been able to find out where the problem is.Please help.
Here is my show_places.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<!-- Show on Map button -->
<Button
android:id="#+id/btn_show_map1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="#string/showplaces"
/>
</LinearLayout>
Here is the ShowPlaces.java file
package com.example.tabswipe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
import android.view.Menu;
public class ShowPlaces extends Activity implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.show_places, container, false);
Button button = (Button) ios.findViewById(R.id.btn_show_map);
button.setOnClickListener(new OnClickListener()
{ #Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
Intent openStartingPoint = new Intent("com.example.tabswipe.Android");
startActivity(openStartingPoint);
}
});
return ios;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Looks like you have android:id="#+id/btn_show_map1" in the xml but are trying to find the id btn_show_map. These one should be changed to be the same as the other.
I'm doing this tutorial, but I'm having some trouble with understanding how to make this into one activity. (In the tutorial, you would have Main Activity and Display Message Activity, but for my purposes, I need it to be in one activity) I understand how everything works, but I just can't figure out how to make this into one activity. Any help is appreciated!
Here is the tutorial I'm working on: https://developer.android.com/training/basics/firstapp/building-ui.html
And I'll include my code below, but it's probably easier to see where I am in the tutorial (I'm at the step, "Starting Another Activity").
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java
package com.example.helloworld;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.content.Intent;
import android.os.Build;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
If you want to change the layout when a button is pressed and display hello world you can use your fragment PlaceholderFragment inside your MainActivity to display it
sample:
public void sendMessage(View view) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Fragment:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView tx = (TextView)rootView.findViewById(R.id.hello_world);
tx.setText(editText.getText().toString());
return rootView;
}
}
In your fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/start_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
I am trying to do something very basic and I cannot figure out why it will not find the ImageView from the Java code. When I type R.id.scanButton I get an error saying that resource does not exist.. but as you can see in my XML it has been created. What am I missing here?
MainActivity.java
package com.erinkabbash.coolmeter;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView button = (ImageView) findViewById(R.id.scanButton); // ImageView not Found
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.erinkabbash.coolmeter.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/resultView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanButton" />
</LinearLayout>
</RelativeLayout>
import R.java
com.erinkabbash.coolmeter.R; //Your.base.pkg.com.R /* You base package .R*/
And clean your project. it might be work,
If you working with your base package domain ,then there is no need of importing R.java. just Clean your project or workspace. . It will work.
Only use lowercase and underscore for res/, drawables/ and others res fies
There is nothing wrong in your code, You just need to Clean and Build your project to register your id that was added.
resultView and scanButton should be lower-cased to resultview and scanbutton. The R.java file is very picky about upper-case letters and special characters anywhere in your res folder.
This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
Edit: I changed my content view to setContentView(R.layout.fragment_main); the application still crashes. I do not quite understand the reason for this.
This is a simple application for detecting and counting the number of potholes felt on a road. I have read many threads on this issue and implemented many different changes to my code. But none seem to work out in fixing the error that I have. Where exactly am I going wrong in my code here?
MainActivity.java
package com.example.potholedetector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements Button.OnClickListener {
// Private member field to keep track of the number of potholes
private static int num_potholes = 0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
//Restore any saved state
super.onCreate(savedInstanceState);
//Set content view
setContentView(R.layout.activity_main);
//Initialize UI elements
final Button pothole = (Button)findViewById(R.id.pothole);
final TextView NumPotholes = (TextView)findViewById(R.id.NumPotholes);
//Link UI elements to actions in code
pothole.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v)
{
num_potholes++;
NumPotholes.setText("Number of potholes felt: " + num_potholes);
}
});
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
/** Called when the user clicks the Pothole button */
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.potholedetector.MainActivity$PlaceholderFragment" >
<TextView android:id="#+id/pothole_message"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/pothole_message"
android:textSize="20sp"/>
<Button android:id="#+id/pothole"
android:layout_width="wrap_content"
android:minWidth="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/pothole"
android:onClick="potholeFelt"/>
<TextView android:id="#+id/NumPotholes"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="20sp"/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">PotholeDetector</string>
<string name="pothole_message">Pothole felt?</string>
<string name="pothole">Pothole</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
I tried to carefully follow many tutorials, but after spending so much time debugging, I am no closer to eradicating the error and moving on. How can I make this code work?
Your app is crashing for NPE, since you are passing as parameter R.layout.activity_main to setContentView, but the elements you are looking for are declared inside fragment_main.xml. Call setContentView(R.layout.fragment_main);