1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 覆写WebViewClient中的onReceivedError()方法:
/** * 显示自定义错误提示页面,用一个View覆盖在WebView */ protected void showErrorPage() { LinearLayout webParentView = (LinearLayout)mWebView.getParent(); initErrorPage(); while (webParentView.getChildCount() > 1) { webParentView.removeViewAt(0); } LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); webParentView.addView(mErrorView, 0, lp); mIsErrorPage = true; } protected void hideErrorPage() { LinearLayout webParentView = (LinearLayout)mWebView.getParent(); mIsErrorPage = false; while (webParentView.getChildCount() > 1) { webParentView.removeViewAt(0); } } protected void initErrorPage() { if (mErrorView == null) { mErrorView = View.inflate(this, R.layout.online_error, null); Button button = (Button)mErrorView.findViewById(R.id.online_error_btn_retry); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { mWebView.reload(); } }); mErrorView.setOnClickListener(null); } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mErrorView.setVisibility(View.VISIBLE); super.onReceivedError(view, errorCode, description, failingUrl); }
|