androideclipseandroid-activityandroid-optionsmenu

Android: start activity from options menu


I am trying to start an activity from an options menu, but my app keeps crashing. The only error that I receive is an ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,Intent) error in the debug window in Eclipse.

Below is the the code that I am using at the moment, but keep in mind I have tried multiple options, all of which end in the same misery, at the same piece of code - the startActivity statement (discovered by using breakpoints, since I'm not sure how to see the stack trace in the LogCat window, as described in my previous question Android/Eclipse: assistance with LogCat).

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.changescheme, menu);
    menu.findItem(R.id.changeScheme).setIntent(new Intent(this, ColourActivity.class));
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    this.closeOptionsMenu();
    startActivity(item.getIntent());
    return true;
}

And here is the changescheme.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/changeScheme" android:title="Change Colour Scheme" android:icon="@android:drawable/ic_menu_edit"></item>
</menu>

I have also tried using a switch(item.getItemId()) statement in the onOptionsItemSelected block as opposed to the menu.findItem in the onCreateOptionsMenu block, but still no luck.

I have defined the activity in my Manifest file. I can also start the activity from a regular button, and the first time the app opens on a device, the activity is started immediately after my splash screen, and I have had no problems with either of these methods.

To me this indicates that there is nothing wrong with the ColourActivity class or its associated layout file, but there is a problem with the implementation from the options menu.

I have also implemented this same method as shown above (in code) in a different app and had no problems, so I'm am really at a loss here.


Solution

  • I have solved the problem now.

    It turns out the problem was not at all on the ListActivity class, it was in fact on the ColourActivity class.

    I was attempting to parse a few colours in onCreate, but I had forgotten to include the # in one of the RGB colour strings, hence the crash!

    Thanks heaps for everyone's help, Adam.