我最近在分析中遇到了奇怪的例外情况。
java.lang.NullPointerException
at com.android.internal.policy.impl.PhoneWindow.onKeyUpPanel(PhoneWindow.java:1002)
at com.android.internal.policy.impl.PhoneWindow.onKeyUp(PhoneWindow.java:1703)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2114)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3626)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3596)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2839)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4904)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我错过了我可以采取哪些措施来防止这种情况。
答案 0 :(得分:22)
在LG解决此问题之前(可能需要几个月,具体取决于他们发布新固件需要多长时间),以下解决方法对我有用。只需将以下代码添加到Activity
类:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && "LGE".equalsIgnoreCase(Build.BRAND)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && "LGE".equalsIgnoreCase(Build.BRAND)) {
openOptionsMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
答案 1 :(得分:7)
在LG设备https://code.google.com/p/android/issues/detail?id=78154
上看起来像支持库中的错误答案 2 :(得分:0)
我们实施了Alex答案的变体。由于该错误仅出现在4.1设备上,因此我们不希望在以后的版本中覆盖任何内容:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mightCrash(keyCode) || super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
if (mightCrash(keyCode)) {
openOptionsMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* See https://code.google.com/p/android/issues/detail?id=78154
*/
private boolean mightCrash(int keyCode) {
return keyCode == KeyEvent.KEYCODE_MENU && "LGE".equalsIgnoreCase(Build.BRAND) && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN;
}