21

I have an Android application with the following menu item in one of the Activities (which concerns handling a list of names and mac numbers):

<item android:id="@+id/menu_sort_tagg"
      android:icon="@android:drawable/ic_menu_sort_by_size"
      android:title="@string/menu_sort_list" >
      <menu> 
        <group android:checkableBehavior="single">
            <item android:id="@+id/sort_by_name"
                  android:title="@string/sort_by_name" />
            <item android:id="@+id/sort_by_mac"
                          android:title="@string/sort_by_mac" />

     </menu>
</item>

and as the application state changes, I want to be able to pre-check which item in the sort options list that was used last time with the following code:

((MenuItem)findViewById(R.id.sort_by_name)).setChecked(true);

The problem is that this specific line gives me a runtime exception. Does anyone have a clue why?

A look at the log reveals that the runtime exceptions is triggered by a null pointer exception. By changing the code in this way:

MenuItem mi = (MenuItem)findViewById(R.id.sort_by_name);
mi.setChecked(true);

it becomes clear that the exception occurs in the seconds statement, i.e., the MenuItem mi is null. So why fails the first statement to bring a pointer to the correct MenuItem?

Robert Granat
  • 211
  • 1
  • 2
  • 4
  • Hi, can you show us a log with the exception? – Cata May 27 '11 at 09:22
  • Sure, here is the error log output: 11:38:34.562: ERROR/AndroidRuntime(14371): FATAL EXCEPTION: main 05-27 11:38:34.562: ERROR/AndroidRuntime(14371): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.classis.safe.android/se.classis.safe.android.TagListActivity}: java.lang.NullPointerException 05-27 11:38:34.562: ERROR/AndroidRuntime(14371): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 05-27 11:38:34.562: ERROR/AndroidRuntime(14371): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 05-27 11:38:34.562: ERROR – Robert Granat May 27 '11 at 09:39
  • It seem to come from a null pointer exception. Could the findViewById faile to provide a pointer to the menuitem we want to check? – Robert Granat May 27 '11 at 09:42
  • Yes I think the problem is that, are you sure your xml is correct? – Cata May 27 '11 at 09:44
  • I think you forget a tag after the last item of the menu.. – Cata May 27 '11 at 09:46

2 Answers2

56

You can't do findViewById() for a menu, because it's a menu, not a view. And you can change menu state when it's being created or prepared. For example, if you create an options menu, you can do it in the Activity: onPrepareOptionsMenu() method:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.sort_by_name).setChecked(true);
    //Also you can do this for sub menu
    menu.getItem(firstItemIndex).getSubMenu().getItem(subItemIndex).setChecked(true);
    return true;
}
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
Michael
  • 53,859
  • 22
  • 133
  • 139
6
private boolean _isHidden = false;

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId())
    {
        case R.id.hiddenfiles:
            if(!_isHidden)
            {
                _isHidden = true;
                item.setChecked(true);
            }
            else {
                _isHidden = false;
                item.setChecked(false);
            }
    }

    return super.onOptionsItemSelected(item);
}
  • You can use this code one or multiple menuitems.

  • Just use 'item' from 'public boolean onOptionsItemSelected(MenuItem item)'

  • I used this, which worked for me. :)

Keyur Sureliya
  • 164
  • 3
  • 11