Android中的WebView后退按钮问题?

时间:2011-07-29 08:07:43

标签: android

我在我的应用中使用了webview。

代码:

    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);
}

当我按下后退按钮时,应用程序崩溃并在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)

我想回到webview中的上一个网页。

1 个答案:

答案 0 :(得分:0)

也许这篇文章可以帮到你:

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

此致