android - OnRecivedError 不显示自定义错误页面

标签 android webview android-webview

我将 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");
    }
}

最佳答案

你可以改变

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,供历史引用。

关于android - OnRecivedError 不显示自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492570/

相关文章:

android - 将填充/边距设置为主题中的顶部 Activity/fragment 布局容器?

javascript - 当页面完全加载完成时显示 Webview?

Android从外部存储读取html文件

android - 启动特定 PreferenceScreen 的 Intent

Android webview 有时会加载内容

android - 限制 Google Play 游戏 API 调用

android - React Native WebView 加载错误处理

android - 如何禁用在 Android 的 webview 中选择整个 Canvas ?

javascript - 第一次运行应用程序时,使用 WebView.loadUrl 执行 javascript 不起作用

android - reCAPTCHA 未在 Android Webview 中呈现