我在webView
中加载了一个网址,当禁用互联网连接显示对话框然后我按下后退按钮让我崩溃:
public class Guide extends AppCompatActivity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeGreen);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.guide);
this.webview = (WebView) findViewById(R.id.webviewGuide);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT >= 19) {
// chromium, enable hardware acceleration
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(Guide.this).create();
progressBar = ProgressDialog.show(Guide.this, getResources().getString(R.string.Waite), "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(Guide.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(MYURL);
}
@Override
public void onDestroy() {
super.onDestroy();
if (webview != null) {
webview.getSettings().setJavaScriptEnabled(false);
webview.clearCache(true);
webview.clearHistory();
}
}
}
这是我的日志:
04-22 21:57:05.174 10307-10854/net.tebyan.ir.sunofneinava E/cutils-trace: Error opening trace file: No such file or directory (2)
04-22 21:57:14.654 10307-10307/net.tebyan.ir.sunofneinava E/Main: Error: Couldn't find the URL.
04-22 21:57:14.714 10307-10307/net.tebyan.ir.sunofneinava E/AndroidRuntime: FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41cb00b8 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:583)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at net.tebyan.ir.sunofneinava.Guide$1.onReceivedError(Guide.java:83)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:353)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5148)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)