15

So I'm trying to grab my menu items off the ActionBar and set them into some variables to use later. Below is some basic test code that tries to set the variable during the creation of the options menu. When the it launches it crashes with the error:

02-18 12:10:08.109: E/AndroidRuntime(30931): FATAL EXCEPTION: main
02-18 12:10:08.109: E/AndroidRuntime(30931): Process: com.example.slider2, PID: 30931
02-18 12:10:08.109: E/AndroidRuntime(30931): java.lang.IndexOutOfBoundsException: Invalid index 2131230720, size is 1
02-18 12:10:08.109: E/AndroidRuntime(30931):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at java.util.ArrayList.get(ArrayList.java:308)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.view.menu.MenuBuilder.getItem(MenuBuilder.java:656)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.example.slider2.MainActivity.onCreateOptionsMenu(MainActivity.java:22)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.view.Choreographer.doFrame(Choreographer.java:543)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.os.Handler.handleCallback(Handler.java:733)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.os.Looper.loop(Looper.java:136)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at java.lang.reflect.Method.invoke(Method.java:515)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
02-18 12:10:08.109: E/AndroidRuntime(30931):    at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends Activity {

    private MenuItem mRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        mRefresh = menu.getItem(R.id.refresh);
        return super.onCreateOptionsMenu(menu);
    }
}

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_action_refresh"
        android:showAsAction="ifRoom"
        android:title="@string/refresh"/>
</menu>

Any ideas on what I'm doing wrong here? I need to be able to manipulate the menu items beyond just when they are being tapped.

Devin Rodriguez
  • 1,144
  • 1
  • 13
  • 30

2 Answers2

35

MenuItem.getItem(index) take index of the menu item instead of id of menu item so use MenuItem.findItem which take menu item id as:

 mRefresh = menu.findItem(R.id.refresh); //item id
  OR
 mRefresh = menu.getItem(0); //item index
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Ah, so I was using the wrong call. findItem is working well. Is there a way I can also use it to get at the home/up item? – Devin Rodriguez Feb 18 '14 at 20:41
  • @DevinRodriguez : for home item try as `menu.findItem(android.R.id.home);` and same from up item – ρяσѕρєя K Feb 18 '14 at 20:44
  • I tried that with no luck, was hoping there was another option. I'm attempting to animate the home button much like it works with the ActionBarDrawerToggle but with a different menu system. It's a slow work in progress. – Devin Rodriguez Feb 18 '14 at 20:48
  • 1
    OMG! So simple error, but so hard to notice what's wrong if you don't know the difference. Unfortunately both calls accept integer. – Mike Keskinov Jun 26 '18 at 18:25
6

getItem(int) returns a MenuItem at a specific index, while findItem(int) returns a MenuItem corresponding to the resource ID that is given. Call findItem instead of getItem.

http://developer.android.com/reference/android/view/Menu.html#findItem(int)

NasaGeek
  • 2,138
  • 1
  • 15
  • 19