Pressing menu button causes crash in Activity with no ActionBar

前端 未结 2 571
Happy的楠姐
Happy的楠姐 2020-12-08 21:14

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

相关标签:
2条回答
  • 2020-12-08 22:04

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

    0 讨论(0)
  • 2020-12-08 22:14

    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);
    }   
    
    0 讨论(0)
提交回复
热议问题