2

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)
4
  • trying cleaning your project and build again.. Nov 25, 2013 at 8:55
  • Check out that in your application res/menu/main.xml file resides or not ?
    – GrIsHu
    Nov 25, 2013 at 9:01
  • i do have a main.xml in my res/menu/ folder
    – dgzz
    Nov 25, 2013 at 9:26
  • could you please post your main.xml file?
    – fasteque
    Nov 25, 2013 at 9:35

2 Answers 2

11

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.

6
  • im using a theme with no title bar. How would I disable the options without using a theme with title bar?
    – dgzz
    Nov 25, 2013 at 9:57
  • The best way would be to override the menu keypress in this case: stackoverflow.com/a/19320065/1025599
    – Nachi
    Nov 25, 2013 at 10:37
  • if i'm going to use the onKeyDown method, does it mean that I need to override the menu key in every activity of my application?
    – dgzz
    Nov 28, 2013 at 2:46
  • You can define a class called BaseActivity that extends Activity and define the method there. Then have every other activity extend BaseActivity.
    – Nachi
    Nov 28, 2013 at 8:26
  • In this way , we can't use options menu, what if we need to also support showing options menu?
    – nmxprime
    Sep 30, 2014 at 10:07
0
 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.

1
  • I have a menu.xml in my res/menu folder but since I'm not going to support the option menu, I changed the return type. I also tried deleting the whole method but it also gives me an error..
    – dgzz
    Nov 25, 2013 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.