隨時按下後退鍵,我的應用程序崩潰。我嘗試過不同的方式,並且他們都崩潰拋出NullPointerException ....任何想法?Android後退鍵錯誤
方式1:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the BACK key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the BACK key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
方式2:
public void onBackPressed() {
myWebView.goBack();
return;
}
堆棧跟蹤:
11-19 20:13:12.425: E/AndroidRuntime(1963): FATAL EXCEPTION: main
11-19 20:13:12.425: E/AndroidRuntime(1963): java.lang.NullPointerException
11-19 20:13:12.425: E/AndroidRuntime(1963): at com.meanwhileinwv.android.MNWVShow.onBackPressed(MNWVShow.java:27)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.app.Activity.onKeyUp(Activity.java:1983)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.view.KeyEvent.dispatch(KeyEvent.java:1518)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.app.Activity.dispatchKeyEvent(Activity.java:2163)
11-19 20:13:12.425: E/AndroidRuntime(1963): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1747)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2702)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2677)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.view.ViewRoot.handleMessage(ViewRoot.java:1965)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.os.Looper.loop(Looper.java:143)
11-19 20:13:12.425: E/AndroidRuntime(1963): at android.app.ActivityThread.main(ActivityThread.java:4263)
11-19 20:13:12.425: E/AndroidRuntime(1963): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 20:13:12.425: E/AndroidRuntime(1963): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 20:13:12.425: E/AndroidRuntime(1963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 20:13:12.425: E/AndroidRuntime(1963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 20:13:12.425: E/AndroidRuntime(1963): at dalvik.system.NativeStart.main(Native Method)
你能從崩潰中發佈堆棧跟蹤嗎? NullPointerException發生在哪一行? – goto10
我添加了堆棧跟蹤 – comead