首页 > 解决方案 > 如果以前的网页没有在 WEB VIEW 中加载,如何将其更改为我自己的网页?

问题描述

PS我的英语很差,我的文字有错误)))请抱歉!问题是,当加载新页面时,WEBVIEW 显示前一个页面,如果根本没有加载或加载错误,则会显示消息网页不可用,我不想看到全部,但隐藏或用我自己的替换它,这怎么可能实现? https://cloud.mail.ru/public/321U/4DU3gj9dy这是我的 Android Studio 项目。存档密码:myapps123

查找并查看错误重建项目并按照以下步骤操作:将应用程序安装到您的手机/

  1. 启动应用程序。
  2. 打开页面上的任何链接。
  3. 关闭互联网,但不要最小化应用程序!
  4. 打开任何链接。
  5. 单击重新加载两次
  6. 打开互联网,但不要最小化应用程序!(按住游览设备中的主页按钮:-)
  7. 使用防火墙阻止应用程序访问 Internet。
  8. 单击应用程序菜单栏中的房屋按钮。9.点击屏幕。
  9. 单击重新加载并查看错误 - 我想隐藏此错误或在我的屏幕或页面上替换它。

标签: javaandroidandroid-studiowebviewcustom-error-pages

解决方案


您可以在 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();
    
            }
        }

你可以这样写 onReceivedError :

    WebView wv = (WebView) findViewById(R.id.webView);
     wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                suåper.onReceivedError(view, errorCode, description, failingUrl);
           //call loadErrorPage function here
        }
     });

推荐阅读