【问题标题】:OnRecivedError doesn't show the custom error pageOnRecivedError 不显示自定义错误页面
【发布时间】:2015-09-10 03:20:08
【问题描述】:

我正在使用带有自定义错误页面的onReceivedError,以在WebView 中显示互联网何时不可用。下面是我使用的代码。它不起作用。它只是在互联网不可用时显示网页不可用页面。

无论如何,logcat 向我显示了这个错误:

I/chromium﹕ [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (0)

我的代码是

private class myWebViewBrowser extends WebViewClient {
    /*@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }*/
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
        view.loadUrl("file:///android_asset/error.html");
    }
}

【问题讨论】:

  • 我通过在 web 视图上显示片段做了同样的事情

标签: android webview android-webview


【解决方案1】:

你可以改变

view.loadUrl(...);

view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );

在哪里

  • "file:///android_asset/" 是加载 html 的基本 url

  • html 是你的 html 字符串

  • “text/html”是内容的 mime 类型

  • “utf-8”是编码风格

  • 最后一个url为null,供历史参考。

【讨论】:

    【解决方案2】:

    这可能会有所帮助:

    https://groups.google.com/forum/#!topic/android-developers/4g6H0vr5_0E

    尝试以下代码:

    private class myWebViewBrowser extends WebViewClient {
    
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    
        Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
        view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );   
        }
    }
    

    【讨论】:

    • 你知道为什么我的代码不工作吗?我的意思是它有时会工作,所以它不会是view.loadUrl 的问题。它必须是别的东西。
    【解决方案3】:
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
            view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );   
            }
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多