Android webview自定义错误页面

我正在创建使用WebView访问在线网站的应用程序.我被困在我必须添加代码检查页面可用性的地方.
public class SpartanWeb extends Activity {

WebView mWebView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Adds ProgRSS bar Support
    this.getwindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);

    // Makes Progress bar Visible
    getwindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);

    // Get Web view
    mWebView = (WebView) findViewById(R.id.webView1);
    WebSettings websettings = mWebView.getSettings();
    websettings.setJavaScriptEnabled(true);
    mWebView.stopLoading();
    mWebView.clearCache(true);
    mWebView.loadUrl("http://google.com");
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url,String userAgent,String contentdisposition,String mimetype,long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }
    });

    // onProgressChanged
    final Activity MyActivity = this;
    mWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view,int progress) {
            // bar disappear after URL is loaded,and changes string to
            // Loading...
            MyActivity.setTitle("Loading...");
            MyActivity.setProgress(progress * 100); // Make the bar
                                                    // disappear after URL
                                                    // is loaded

            // Return the app name after finish loading
            if (progress == 100)
                MyActivity.setTitle(R.string.app_name);
        }
    });

}// EOM oc

public boolean onKeyDown(int keyCode,KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode,event);
}

}

我试图添加onReceivedError但由于某种原因自定义页面没有加载.

/** Called when the activity is first created. */
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) 
{
mWebView.loadUrl("file:///android_asset/error.html");
}

请告知该怎么做.

解决方法

您可以在onReceivedError函数调用loadErrorPage(view)函数.

以下代码将加载您需要显示错误内容.这里我使用loadDataWithBaseURL加载html文件.

public void loadErrorPage(WebView webview){
        if(webview!=null){

            String htmlData ="<html><body><div align=\"center\" >"This is the description for the load fail : "+description+"\nThe Failed url is : "+failingUrl+"\n"</div></body>";

            webview.loadUrl("about:blank");
            webview.loadDataWithBaseURL(null,htmlData,"text/html","UTF-8",null);
            webview.invalidate();

        }
    }

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...