0

I use a webview in my app.

Code:

    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100){

                activity.setTitle(R.string.app_name);
                //view.canGoBack();
            }
        }
    });


    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            view.canGoBack();
            super.onPageFinished(view, url);
        }

    });
    //The URL that webview is loading
    webView.loadUrl(uri);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;            
            }
        return super.onKeyDown(keyCode, event);
}

when I press the back button pressed, the app is crashes and generates an error in logcat

07-29 12:47:56.341: ERROR/AndroidRuntime(10567): Uncaught handler: thread main exiting due to uncaught exception
07-29 12:47:56.401: ERROR/AndroidRuntime(10567): java.lang.NullPointerException
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at com.MyDemo.WebViewActivity.onKeyDown(SiteViewActivity.java:74)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.view.KeyEvent.dispatch(KeyEvent.java:1069)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.app.Activity.dispatchKeyEvent(Activity.java:1980)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1724)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2424)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2394)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1726)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.os.Looper.loop(Looper.java:123)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at android.app.ActivityThread.main(ActivityThread.java:3948)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at java.lang.reflect.Method.invoke(Method.java:521)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-29 12:47:56.401: ERROR/AndroidRuntime(10567):     at dalvik.system.NativeStart.main(Native Method)

I want to go back to previous web page in webview.

Reno
  • 33,594
  • 11
  • 89
  • 102
Durgesh Patel
  • 326
  • 2
  • 7
  • 20
  • Please share LogCat output. So we can figure out in which line it throws `NullPointerException`. – Onuray Sahin Jul 29 '11 at 08:14
  • thia line is 74 if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) – Durgesh Patel Jul 29 '11 at 08:49
  • The nullpointer is probably because webView is null (please check with logging if this is really the case). Are you deferencing your webView somewhere? In other words is there somewhere something like `webView = null` in your code? – THelper Jul 29 '11 at 09:53
  • @durgesh you should upvote almighty972 answer if it helped you. – Toby Allen Jul 29 '11 at 19:44

1 Answers1

0

maybe this post will help you:

How to go back to previous page if back button is pressed in WebView?

Regards

Community
  • 1
  • 1
almighty972
  • 1,158
  • 2
  • 6
  • 5