[android]删除 onCreateOptionsMenu 到 NullPointerException 的结果
我发现,当按下菜单按钮在我的设备上,它给我的 NullPointerException。
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu); //NullPointer Here
return true;
}
所以自己做了什么是完全禁用菜单通过删除上面的第二行并将返回的类型更改为 false。然而,它仍给我 NullPointerException。这里是的堆栈:
11-25 03:50:20.931 1212-1212/com.sblive.aufschoolbliz E/InputEventSender﹕ Exception dispatching finished signal.
11-25 03:50:20.931 1212-1212/com.sblive.aufschoolbliz E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
11-25 03:50:21.040 1212-1212/com.sblive.aufschoolbliz E/MessageQueue-JNI﹕ java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:274)
at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:89)
at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
at android.app.Activity.onCreatePanelMenu(Activity.java:2505)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:795)
at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1471)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1867)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:132)
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
11-25 03:50:21.040 1212-1212/com.sblive.aufschoolbliz D/AndroidRuntime﹕ Shutting down VM
11-25 03:50:21.040 1212-1212/com.sblive.aufschoolbliz W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-25 03:50:21.150 1212-1212/com.sblive.aufschoolbliz E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:274)
at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:89)
at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
at android.app.Activity.onCreatePanelMenu(Activity.java:2505)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:795)
at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1471)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1867)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:132)
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
解决方法 1:
您的堆栈跟踪显示实际北角是在 ActionBarImplICS.java
:
@Override
public Context getThemedContext() {
return mActionBar.getThemedContext(); // here
}
可以有几个原因为什么您操作栏为空。
- 你不打 onCreate() setContentView()?
- 你缺少至少
android:minSdkVersion="11"
在清单中吗? - 您正在使用的主题,如
android:theme="@style/Theme.NoTitle"
吗? - 您是否正在使用自定义的样式,像
android:windowIsFloating
,android:backgroundDimEnabled
或android:windowNoTitle
?
编辑: 在案例中有没有标题 / 操作栏,解决此问题的最佳方法似乎是通过重写菜单键按下。
比较清楚的一个回答——
不要在程序中写这一句requestWindowFeature(Window.FEATURE_NO_TITLE);,把它换为 ActionBar actionBar=getSupportActionBar(); actionBar.hide();
这两句就行了,这样标题栏就被隐藏起来,而且点击Menu键也不会出错了!应用是要extends ActionBarActivity,不能用默认的extends Activity.
而如果保留活动的标题的话,不但可以使用Menu按键触发菜单,在应用的标题栏的右边也会有一个下拉菜单,效果与使用Menu按键是一样的。