Anytime I press the Back Key, my application crashes. I have tried to different ways, and they both crash throwing a NullPointerException…. any ideas?
Way 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);
}
Way 2:
public void onBackPressed() {
myWebView.goBack();
return;
}
Stack Trace:
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)
The only way to get the stack trace you are getting from the code you’ve shared is for
myWebViewto benullwhenmyWebView.goBack()is called. To prove this to yourself, modify youronBackPressed():The exception should go away. (Though, obviously, you still won’t get any back behavior occurring.)
Possible causes:
myWebViewis not a member variable in your class. You may have declared and set it in another method, which means it’s not in scope of onBackPressed.myWebViewisn’t the actual name of the variable that refers to your WebViewmyWebViewused to refer to your WebView but at some point was set tonullagainmyWebViewto yourWebViewis actually setting tonull. For instance, afindViewByIdcall that is not actually finding theWebViewbecause you’ve specified the wrong ID will returnnull.If you’re still stuck, post the code where
myWebViewis declared and where it is assigned.