如何引用menu.xml中定义的MenuItem?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何引用menu.xml中定义的MenuItem?相关的知识,希望对你有一定的参考价值。
我必须在activity中动态地引用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"/>
答案
试试这个:在菜单上调用findItem()
以找到一个操作栏项,通过使用Activity
向onPrepareOptionsMenu
添加覆盖
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem homeItem= menu.findItem(R.id.home);
homeItem.setIcon(R.drawable.home72);
super.onPrepareOptionsMenu(menu);
return true;
}
以上是关于如何引用menu.xml中定义的MenuItem?的主要内容,如果未能解决你的问题,请参考以下文章
如何在ToolBar中显示文字和图标,自定义图标大小,并和MenuItem关联
android中的NullPointerException,同时在menu.xml中使用actionLayout