4

如果收到错误,我想显示资产中的本地 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);
    }
4

1 回答 1

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);
        }

    });
于 2016-08-03T10:00:32.727 回答