removing onCreateOptionsMenu results to NullPointe

2019-05-29 00:06发布

问题:

I found out that when I press the menu button on my device, it gives me a NullPointerException.

 public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu); //NullPointer Here
        return true;
    }

So what I did is to completely disable the menu by removing the second line above and changing the return type to false. However, it is still giving me a NullPointerException. Here is the stack:

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:

Your stack trace shows that the actual NPE is in ActionBarImplICS.java:

@Override
public Context getThemedContext() {
        return mActionBar.getThemedContext(); // here
}

There can be a few reasons why your action bar is null.

  • Are you not calling setContentView() in onCreate()?
  • Are you missing at least android:minSdkVersion="11" in your manifest?
  • Are you using a theme like android:theme="@style/Theme.NoTitle"?
  • Are you using custom styles like android:windowIsFloating, android:backgroundDimEnabled or android:windowNoTitle?

EDIT: In case there is no title / actionbar, the best way to fix this seems to be by overriding the menu key press.



回答2:

 public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu); //NullPointer Here
        return true;
    }

is created by default for supporting option menu. it inflates the specified "menu R.menu.main" you may have not defined it. If you wish to keep supporting option menu then define a menu as "main.xml" in /res/menu folder, otherwise if you don't want to support option menu just remove the whole method.