1

我必须在活动中动态引用 menu.xml 中定义的项目。

我试图通过以下方式引用:

MenuItem homeItem = (MenuItem) findViewById(R.id.home);
homeItem.setIcon(R.drawable.home72);

它引发了以下异常:

03-31 10:39:01.703: E/AndroidRuntime(2463): FATAL EXCEPTION: main
03-31 10:39:01.703: E/AndroidRuntime(2463): java.lang.ClassCastException: com.android.internal.view.menu.ActionMenuItemView cannot be cast to android.view.Menu
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.emg.emgstorelocator.StoreLocatorActivity.onOptionsItemSelected(StoreLocatorActivity.java:133)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.view.View.performClick(View.java:4084)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.view.View$PerformClick.run(View.java:16966)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.os.Handler.handleCallback(Handler.java:615)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.os.Looper.loop(Looper.java:137)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at java.lang.reflect.Method.invoke(Method.java:511)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-31 10:39:01.703: E/AndroidRuntime(2463):     at dalvik.system.NativeStart.main(Native Method)

这是在 menu.xml 中定义的:

<item 
    android:id="@+id/home"
    android:showAsAction="always"
    android:title="Home"
    android:icon="@drawable/home72"/>
<item 
    android:id="@+id/location"
    android:showAsAction="always"
    android:title="Locations"
    android:icon="@drawable/locations72"/>
4

1 回答 1

7

试试这个:调用findItem()菜单来查找操作栏项目,通过添加覆盖到ActivitywithonPrepareOptionsMenu

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem homeItem= menu.findItem(R.id.home);
    homeItem.setIcon(R.drawable.home72);
    super.onPrepareOptionsMenu(menu);
    return true;
}

更多信息:https://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)

于 2013-03-31T05:29:18.850 回答