pressing菜单按钮导致崩溃的活动,没有动作条按钮、菜单、动作、pressing

2023-09-12 01:45:30 作者:ⅰ场游戏ⅰ场梦

我在Android的一个新手,正在我的第一个应用程序。

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

我有没有动作条在它的主要活动。我不想显示该活动任何菜单。一切都工作得很好,但在我preSS菜单按钮present设备本身,它会导致只是忽略它我的应用程序强制关闭吧。

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.

我正在开发的SDK> = 8所以我使用了支持库。我曾尝试在code有没有在它加入 OnCreateOptionMenu(),但结束了相同的结果。

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.

我的应用程序的名称是GUI和包是GUI。这里是logcat的:

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.

推荐答案

我的猜测是,这是在AppCompat库中的缺陷。如果你看一下在$ C $下ActionBarImplICS.getThemedContext(),你看到它的mActionBar是空:

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://grep$c$c.com/file/repository.grep$c$c.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);

如果我删除此并启动活动有一个标题/动作条我一直没能重现崩溃。现在,当你不想要或需要一个标题栏运行的应用程序是不是一个很好的选择。我的建议是,你重写菜单键preSS。该应用程序停止了崩溃的我,当我这样做:

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