如何访问菜单项?

时间:2014-02-18 20:23:14

标签: android

所以我试图从ActionBar中取出我的菜单项并将它们设置为一些变量以便稍后使用。下面是一些基本测试代码,它尝试在创建选项菜单期间设置变量。当它启动时,它崩溃并出现错误:

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>

关于我在这里做错了什么的想法?我需要能够操作菜单项,而不仅仅是在它们被点击时。

2 个答案:

答案 0 :(得分:28)

MenuItem.getItem(index)获取菜单项的索引而不是菜单项的id,因此请使用MenuItem.findItem将菜单项ID作为:

 mRefresh = menu.findItem(R.id.refresh); //item id
  OR
 mRefresh = menu.getItem(0); //item index

答案 1 :(得分:4)

getItem(int)返回特定索引处的MenuItem,而findItem(int)返回与给定资源ID对应的MenuItem。请致电findItem而不是getItem

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