为什么在触摸硬件菜单时崩溃应用程序?

时间:2015-09-29 03:45:31

标签: android

当我触摸硬件设置菜单时,我的应用程序崩溃了。我在互联网上搜索并找到了这段代码:

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    if (keycode == KeyEvent.KEYCODE_SETTINGS) { //ignore settings key         
        return true;
    } else {
        return super.onKeyDown(keycode, e);
    }
}

我在我的应用程序中使用此代码但是不起作用。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.collection__list_, menu);
    return true;
}

堆栈跟踪:

FATAL EXCEPTION: main Process: com.example.nabege, PID: 5944                                   java.lang.NullPointerException                                       
    at    android.support.v7.app.ActionBarImplICS.setDisplayHomeAsUpEnabled(ActionBarImplICS.java:179)
    at    android.support.v7.app.ActionBarImplJB.setDisplayHomeAsUpEnabled(ActionBarImplJB.java:20)
    at    android.support.v7.app.ActionBarActivityDelegate.getSupportActionBar(ActionBarActivityDelegate.java:86)
    at    android.support.v7.app.ActionBarActivityDelegate.getActionBarThemedContext(ActionBarActivityDelegate.java:206)
    at    android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98)
    at    android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
    at    com.example.nabege.Collection_List_Activity.onCreateOptionsMenu(Collection_List_Activity.java:202)    at android.app.Activity.onCreatePanelMenu(Activity.java:2625)       
    at    android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
    at    android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
    at    android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
    at    android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)    at    android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
    at    com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:505)
    at    com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:894)
    at    com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1671)
    at    com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2181)
    at    android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4492)
    at    android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4459)
    at    android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024) 
    at    android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4078)
    at    android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4047) 
    at    android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4158)
    at    android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4055)
    at    android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4215)
    at    android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024) 
    at    android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4078)
    at    android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4047) 
    at    android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4055)
    at    android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024) 
    at    android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4078)
    at    android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4047) 
    at    android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4191)
    at    android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4351)
    at    android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2257)
    at    android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1907)
    at    android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1898)
    at    android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2234)
    at    android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
    at android.os.MessageQueue.nativePollOnce(Native Method)            
    at android.os.MessageQueue.next(MessageQueue.java:138)              
    at android.os.Looper.loop(Looper.java:123)                          
    at android.app.ActivityThread.main(ActivityThread.java:5356)        
    at java.lang.reflect.Method.invokeNative(Native Method)             
    at java.lang.reflect.Method.invoke(Method.java:515)                 
    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)    
    at dal

2 个答案:

答案 0 :(得分:0)

这个错误可能来自你的R.menu.collection__list,它有一些错误,我有一个类似的问题,那就是手机的低堆内存,当它想要在对话中午餐或活动或菜单,为此,我不得不降低我的布局中的背景图像和其他图片的分辨率,并得到修复。 希望对你有帮助。

答案 1 :(得分:0)

看起来你正在隐藏行动栏。

所以getSupportActionBar()正在返回null。然后,您尝试访问它,无论是故意还是系统,都可以设置Home As Up。

这会导致NullPointerError

如果您不隐藏操作栏,该应用程序是否有效?