Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

android - Pressing menu button causes crash in Activity with no ActionBar

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...