别的不多说了 注意一点在处理的时候需要兼容新老版本,还有就是 判断一下错误的URL和当前加载的URL是否一致:
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return;
}
if (webView.getUrl().equals(failingUrl)) {
view.loadUrl("file:///android_asset/web404.html");//自定义界面
}
}
// 新版本,只会在Android6及以上调用
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (request.isForMainFrame()) { // 或者: if(request.getUrl().toString() .equals(getUrl()))
view.loadUrl("file:///android_asset/web404.html");
}
}