防止android webview显示错误页面

时间:2013-07-05 12:58:33

标签: android webview android-webview

如果收到错误,我想从资产中显示本地HTML。但是,即使我覆盖onReceivedError方法,在显示资产页面之前,也会有一个错误页面闪烁一秒钟。这个问题有解决方法还是黑客攻击?

这是我的代码:

    @Override
    public void onReceivedError (WebView view, int errorCode, String description, String failingUrl)
    {
        Log.i("onReceivedError", "onReceivedError: " + failingUrl + " errorCode: " + errorCode);
        super.onReceivedError(view, errorCode, description, failingUrl);
        view.stopLoading();
        loadFromAsset(view, failingUrl);
    }

1 个答案:

答案 0 :(得分:0)

我能够通过检查onPageStarted事件中是否存在互联网连接来绕过此问题,现在它似乎正在我们测试的所有设备上工作。我正在使用自定义HTML错误页面。

 mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if(!NetworkConnection.isNetworkAvailable(getActivity()) && !url.equals("file:///android_asset/error_page.html")){
                view.loadUrl("file:///android_asset/error_page.html");
            }
            super.onPageStarted(view, url, favicon);
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            mWebView.loadUrl("file:///android_asset/error_page.html");
            super.onReceivedError(view,errorCode,description,failingUrl);
        }

    });