18

I'm a newbie in Android and working on my first app.

I have the main activity with no ActionBar in it. And I don't want to display any menu in that Activity. Everything is working just fine But when I press the menu button present in the device itself, it causes my app to force close instead of just ignoring it.

I'm developing for sdk >=8 so I'm using the support library. I have tried adding OnCreateOptionMenu() in the code with nothing in it but ended up with same results.

The name of my app is GUI and the package is gui. Here is the Logcat:

10-09 19:52:32.920: E/AndroidRuntime(7440): FATAL EXCEPTION: main
10-09 19:52:32.920: E/AndroidRuntime(7440): java.lang.NullPointerException
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:274)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:89)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.app.Activity.onCreatePanelMenu(Activity.java:2554)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:453)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:853)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1535)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2052)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3924)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3872)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3007)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.os.Looper.loop(Looper.java:137)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at android.app.ActivityThread.main(ActivityThread.java:4921)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
10-09 19:52:32.920: E/AndroidRuntime(7440):     at dalvik.system.NativeStart.main(Native Method)

I can't figure out what's the cause of this error. Please help me to locate it. If any other info is needed then plz let me know.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Anjani
  • 1,533
  • 3
  • 15
  • 26

2 Answers2

49

My guess is that this is a bug in the AppCompat library. If you take a look at the code for ActionBarImplICS.getThemedContext() you see that it's the mActionBar that is null:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/android/support/v7/app/ActionBarImplICS.java#ActionBarImplICS.getThemedContext%28%29

My guess is that you're using an activity without a title (and thus also without an actionbar):

requestWindowFeature(Window.FEATURE_NO_TITLE);

If I remove this and launch the activity with a title/actionbar I haven't been able to reproduce the crash. Now, running the app with a titlebar when you don't want or need one isn't a very good option. My suggestion is that you override the Menu key press. The app stopped crashing for me when I did this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ( keyCode == KeyEvent.KEYCODE_MENU ) {
        // do nothing
        return true;
    }
    return super.onKeyDown(keyCode, event);
}   
britzl
  • 10,132
  • 7
  • 41
  • 38
  • 1
    That just worked perfectly for me. Yes i had created a custom theme for my activity with no title bar. And as u rightly pointed, the cause of the error was most probably the getThemedContext() function. Thank you so much for your help. I hope this would come handy to others as well. – Anjani Oct 12 '13 at 14:23
  • In case someone bumps into the same problem: I am using a TabMenu, so I had to add this code in ALL the views in order to actually catch the event. – Teo Inke Jun 06 '14 at 06:38
  • @britzl: My doubt is, I have implemented the same actionbar for other activity also. Then why it is not crashing over there? – Vikas B L Aug 21 '14 at 05:06
  • I mean without actionbar. It is working everywhere except a particular activity, where there was actionbar before I modified to no actionbar. – Vikas B L Aug 21 '14 at 05:08
  • Sorry I didn't check it properly. It is there in all other activity. – Vikas B L Aug 21 '14 at 05:49
  • @britzl, I am using navigation drawer with action bar, and adding searchview on the activity, its crash with NPE on android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293) – Abdul Wahab Nov 20 '14 at 06:18
0

Looks like this will be fixed in the next support library release http://code.google.com/p/android/issues/detail?id=61394

Eliezer
  • 7,209
  • 12
  • 56
  • 103