tags:

views:

42

answers:

3

I know this has been covered elsewhere, but I'm new to the Android platform and am having a hard time figuring out how to add basic menu options to my first app.

I have an options menu setup using

@Override
public boolean onCreateOptionsMenu(Menu menu){
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.menu, menu);
  return true;
}

and

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item android:id="@+id/menuPrefs" android:icon="@android:drawable/ic_menu_preferences" android:title="Settings"></item>
</menu>

Then within my main java class I have

@Override
public boolean onOptionsItemSelected(MenuItem item){

 if(item.getItemId()==R.id.menuPrefs) {
  showPrefs();
 }
private void showPrefs() {
 Intent i = new Intent(this, Prefs.class);
 startActivity(i);
}

And then in Prefs.java I have

public class Prefs extends Activity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      Toast.makeText(getBaseContext(), "FNORD", Toast.LENGTH_LONG).show();
     }
    }

Now from this I would expect to see the Toast message "FNORD" when the menu option is pressed, however the application stops unexpectedly.

If I move the toast statement into the showPrefs() function in place of the startActivity call it works.

A: 

You need to learn how to Debug in Eclipse and how to use the ADB and DDMS tools.

In order to get more details about an exception/force close you need to look for a view in Eclipse called Logcat(you will find in the DDMS perspective) there you will find a detailed traceback when/what and on what line is the issue.

For this you should read a complete article about Debugging in Android using Eclipse

alt text

Pentium10
The link you provided fails to open, however I did manage to find the source of the problem. "android.content.ActivityNotFoundException: Unable to find explicit activity class", so I need to declare the activity in AndroidManifest.xml. Forgive my ignorance, but how does one go about doing that? Having a hard time finding a good example.
Kujako
Never mind, simply added <activity android:name="Prefs"></activity> to the manifest and that seems to have done the trick.
Kujako
You have a low rate. Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10
+1  A: 

You forgot to call super.onCreate(Bundle savedInstanceState); on Prefs' onCreate() method.

alex
@alex: When I read this I asked myself: where?. So I added to your answer.
Macarse
I assume you mean "super.onCreate(savedInstanceState);". Adding it has no effect on the crash. I removed it and all code other then the Toast call in the Prefs.java onCreate function to try and eliminate potential causes, to no avail.
Kujako
A: 

Solution was that I needed to add the Prefs.xml to the manifest.

Kujako