I use a webview in my app.
Code:
webView.getSettings().setJavaScriptEnabled(true);
开发者_如何学Go 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.
maybe this post will help you:
How to go back to previous page if back button is pressed in WebView?
Regards
精彩评论