Reputation: 1164
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.
Upvotes: 15
Views: 11637
Reputation: 132972
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
Upvotes: 35
Reputation: 2198
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)
Upvotes: 6