Nick Nelson
Nick Nelson

Reputation: 1263

NullPointerException on Back Button Press

I am in the process of updating my app and right now I am getting NullPointerException when I press the back button. Here is my main activity:

image http://www.appinfluence.com/wp-content/uploads/2011/11/screenshot1.png image2 http://www.appinfluence.com/wp-content/uploads/2011/11/screenshot2.png

When clicking music my second activity is launched and it has a webview nested inside it. I am having issues getting the back button to work properly. I expect it to go back in the browser until getting to the first page and then back to the first activity once it can't go back anymore. Here is my code, and log file.

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


11-03 16:41:14.386: W/dalvikvm(282): threadid=1: thread exiting with uncaught exception     (group=0x4001d800)
11-03 16:41:14.486: E/AndroidRuntime(282): FATAL EXCEPTION: main
11-03 16:41:14.486: E/AndroidRuntime(282): java.lang.NullPointerException
11-03 16:41:14.486: E/AndroidRuntime(282):  at     com.appinfluence.musicpromotion.WebViewActivity.onKeyDown(WebViewActivity.java:315)
11-03 16:41:14.486: E/AndroidRuntime(282):  at     android.view.KeyEvent.dispatch(KeyEvent.java:1037)
11-03 16:41:14.486: E/AndroidRuntime(282):  at     android.app.Activity.dispatchKeyEvent(Activity.java:2068)
11-03 16:41:14.486: E/AndroidRuntime(282):  at     com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:16    43)
11-03 16:41:14.486: E/AndroidRuntime(282):  at    android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
11-03 16:41:14.486: E/AndroidRuntime(282):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
11-03 16:41:14.486: E/AndroidRuntime(282):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
11-03 16:41:14.486: E/AndroidRuntime(282):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 16:41:14.486: E/AndroidRuntime(282):  at android.os.Looper.loop(Looper.java:123)
11-03 16:41:14.486: E/AndroidRuntime(282):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-03 16:41:14.486: E/AndroidRuntime(282):  at java.lang.reflect.Method.invokeNative(Native Method)
11-03 16:41:14.486: E/AndroidRuntime(282):  at java.lang.reflect.Method.invoke(Method.java:521)
11-03 16:41:14.486: E/AndroidRuntime(282):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-03 16:41:14.486: E/AndroidRuntime(282):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-03 16:41:14.486: E/AndroidRuntime(282):  at dalvik.system.NativeStart.main(Native Method)

You guys are right my webview is null, BUT WHY?

WebView mWebView;
WebView mWebView = (WebView) findViewById(R.id.webview);

changed to this (OOPS!):

WebView mWebView;
mWebView = (WebView) findViewById(R.id.webview);

Upvotes: 0

Views: 3126

Answers (4)

Mateusz
Mateusz

Reputation: 2317

Wrap the if code in something like this:

if(mWebView!=NULL){ your if } 

and see what happens if nothing than mWebView is null, also you can print some debug.

Upvotes: 0

BrianPlummer
BrianPlummer

Reputation: 164

Check to make sure your variable is not null. I can't tell exactly what line it is dying on, but I am assuming it is getting the null-pointer on "mWebView.canGoBack()" in the IF condition.

Upvotes: 0

FunkTheMonk
FunkTheMonk

Reputation: 10938

Assuming com.appinfluence.musicpromotion.WebViewActivity.onKeyDown is the onKeyDown method you posted, mWebView is null for some reason.

Upvotes: 0

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

I'm guessing it's your mWebView that is null. Have you made sure it's initialized when this method is being called? You could try just throwing in a quick test before calling mWebView.canGoBack() and print out the results to the log like so:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(mWebView == null){
      log.d("My Tag", "Webview is null on KeyCode: " + String.valueOf(keyCode));
    }
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 1

Related Questions