我使用以下代码创建了普通的选项菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 0, "A");
menu.add(0, 2, 0, "B");
menu.add(0, 3, 0, "C");
return true;
}
在Android 2.3及以下版本(2.2、2.1),一切都正常。但是在Android 4.0(ICS - 模拟器、Galaxy Nexus、Nexus S)上运行此应用程序时,当我按下菜单按钮时,应用程序会崩溃并出现以下异常:
W/ResourceType(9263): Failure getting entry for 0x01090044 (t=8 e=68) in package 0 (error -75)
D/AndroidRuntime(9263): Shutting down VM
W/dalvikvm(9263): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x1090044
E/AndroidRuntime(9263):
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.internal.view.menu.BaseMenuPresenter.getMenuView(BaseMenuPresenter.java:70)
at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.getIconMenuView(PhoneWindow.java:3298)
at com.android.internal.policy.impl.PhoneWindow.initializePanelContent(PhoneWindow.java:1096)
at com.android.internal.policy.impl.PhoneWindow.openPanel(PhoneWindow.java:559)
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:817)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1486)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1813)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
我无法找到ID为#0x1090044的资源(它不在我的R.java或任何其他文件中),我也不知道它应该是什么。也许是一些内部菜单布局?我已经尝试过清理项目了。 我知道Android 4.0 ICS有新的“设计规则”,使用操作栏而不是旧菜单,但出于许多不同原因,我需要使这段代码即使在ICS上也能正常工作。我应该如何编辑此代码以使其即使在ICS上也能正常工作? 我还尝试使用相同异常创建来自XML文件的菜单的代码。
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
感谢您提供的任何建议!
getMenuView
中,目前没有办法拦截并停止它。在我的情况下,我只是删除了选项菜单的使用。 - Femi