0

I have a menu -

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.example.ju_group.health_assist.MainActivity">
    <item
        android:id="@+id/help"
        android:checkable="false"
        android:icon="@android:drawable/ic_menu_help"
        android:title="Help"
        app:showAsAction="ifRoom"/>
    <group
        android:id="@+id/disable"
        android:checkableBehavior="all"
        android:visible="true">
        <item
            android:id="@+id/dis_new"
            android:title="Disable PopUp"
            android:visible="true"/>

    </group>
</menu>

So I am accessing the item dis_new in-

    getMenuInflater().inflate(R.menu.menu_main, menu);
    if(disable_popup==1)
        menu.getItem(R.id.dis_new).setChecked(true); //here I'm getting error
        return true;  //we have inflated the menu
    }

disable_popup is earlier set to 1 or 0, depending on certain conditions.
Now I am getting an IndexOutOfBound Exception when I set the item checked.
But many times it's getting executed correctly(with both disable_popup values 0 and 1).

java.lang.IndexOutOfBoundsException: Invalid index 2131624239, size is 2

is the exception message.

I used try-catch to contain the error and taken appropriate steps, but I want to know what is causing the error.

EDIT: stacktrace

FATAL EXCEPTION: main
  Process: com.example.*******, PID: 829
  java.lang.IndexOutOfBoundsException: Invalid index 2131624239, size is 2
 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
 at java.util.ArrayList.get(ArrayList.java:308)
 at android.support.v7.view.menu.MenuBuilder.getItem(MenuBuilder.java:741)
 at com.example.*****.SymptomsAnalyze.onCreateOptionsMenu(SymptomsAnalyze.java:113)
 at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
 at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
 at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
 at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
 at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
 at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
 at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
 at android.os.Handler.handleCallback(Handler.java:733)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5017)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 at dalvik.system.NativeStart.main(Native Method)
vinS
  • 1,417
  • 5
  • 24
  • 37
string24
  • 15
  • 6

2 Answers2

2

the getItem is expecting a position, not an id change this line

  menu.getItem(R.id.dis_new) to this menu.getItem(1)
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
1

Method menu.getItem(int) is for accessing menu item by its index(for example: 0, 1, 2). If you want to access menu item by id, you should use menu.findItem(int).

getMenuInflater().inflate(R.menu.menu_main, menu);

    if(disable_popup==1)
         menu.findItem(R.id.dis_new).setChecked(true); //findItem() instead of getItem()

    return true;  //we have inflated the menu
}
Karol Jurski
  • 180
  • 1
  • 10