【问题标题】:Hide Webview errors in android在android中隐藏Webview错误
【发布时间】:2013-01-16 08:50:25
【问题描述】:

我还没有找到任何特定的解决方案来隐藏 Android 中 webviews 显示的默认错误。我可以通过错误代码捕获特定错误来显示我自己的自定义错误消息。

这里的问题是,在我的自定义错误消息显示之前,我会在一瞬间看到 WebView 错误,然后显示我的自定义错误。

以下是一段代码,用于处理错误并显示我自己的自定义错误消息:

protected void onPostExecute(String S) {

        mWebView.setWebViewClient(new WebViewClient() { 


            @Override
            public void onReceivedError(WebView view, int errCode, String errDescription, String failingUrl ) {

                view.clearView();

                Toast.makeText(getApplicationContext(), "Error code is "+errCode, Toast.LENGTH_SHORT).show();
                if(errCode == -2 || errCode == -8) {
                    view.loadData("There seems to be a problem with your Internet connection. Please try later", "text/html", "UTF-8");
                }

                if(errCode == -14) {
                    view.loadData("Page cannot be found on server", "text/html", "UTF-8");
                }

            }

        });

        mWebView.loadUrl(url);

        ShowProgress.dismiss();
    } 

有人可以就如何实现隐藏 webview 错误并仅显示我的自定义错误消息提出任何修改或建议吗?感谢您光临并阅读这篇文章。

【问题讨论】:

    标签: android error-handling android-webview


    【解决方案1】:

    尝试添加这个:

    view.stopLoading();
    

    你的源代码会是这样的:

    protected void onPostExecute(String S) {
    
            mWebView.setWebViewClient(new WebViewClient() { 
    
    
                @Override
                public void onReceivedError(WebView view, int errCode, String errDescription, String failingUrl ) {
    
                    try {
                           view.stopLoading();
                      } 
                    catch(Exception e){}
                    view.clearView();
    
                    Toast.makeText(getApplicationContext(), "Error code is "+errCode, Toast.LENGTH_SHORT).show();
                    if(errCode == -2 || errCode == -8) {
                        view.loadData("There seems to be a problem with your Internet connection. Please try later", "text/html", "UTF-8");
                    }
    
                    if(errCode == -14) {
                        view.loadData("Page cannot be found on server", "text/html", "UTF-8");
                    }
    
                }
    
            });
    
            mWebView.loadUrl(url);
    
            ShowProgress.dismiss();
        } 
    

    【讨论】:

    • 感谢您的回复。我按照你的方法试过了。我仍然面临同样的问题。最初的错误仍然会出现一瞬间。
    • 尝试进入这个讨论code.google.com/p/android/issues/detail?id=2340。它可以帮助
    • 谢谢。我已经看到了这个,甚至想过在那里发帖。最后,现在我离开了我的 cmets。好像很久没有更新了。谢谢你的时间。
    【解决方案2】:

    我找不到具体的解决方案,因为这是一个错误 #2340。因此,我将 webview 从应用程序中取出并改用常规浏览器。

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多