我的应用在 ICS emu 上运行良好。但是,使用 Gingerbread 时,只要我的 onCreateOptionsMenu 正在使用,它就会崩溃。如果我改用 showPopup,它还会引发另一组类似但不同的错误。有谁知道这里到底发生了什么?理想情况下,我希望有一点菜单代码(当然不包括监听器),它将为运行应用程序的所有版本的 android 布局一个菜单。不过,我应该提一下,我有一个 GUI 菜单按钮(作为 ImageView 位于我的 XML 文件中),而不是 ActionBar 中的一个。所以,这里是代码:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
这是菜单 XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/demographics"
android:title="@string/demographics" />
<item android:id="@+id/tabSelection"
android:title="@string/tabs" />
<item android:id="@+id/settings"
android:title="@string/settings" />
</menu>
这是 LogCat:
threadid=1: thread exiting with uncaught exception (group=0x40015560)
FATAL EXCEPTION: main
java.lang.IllegalStateException: Cannot interact with object designed for temporary instance passing. Make sure you using both SherlockFragmentActivity and SherlockFragment.
at com.actionbarsherlock.internal.view.menu.MenuMule.add(MenuMule.java:40)
at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:310)
at android.view.MenuInflater.parseMenu(MenuInflater.java:154)
at android.view.MenuInflater.inflate(MenuInflater.java:80)
at com.davekelley.polling.Polling.onCreateOptionsMenu(Polling.java:203)
at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:209)
at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:236)
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:543)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:467)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:269)
at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(ActionBarSherlockCompat.java:972)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)