无法在webview中看到onReceivedError

时间:2017-12-20 00:01:42

标签: android

我在旧硬件中遇到问题无法看到此代码

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                mywebsite.loadUrl("about:blank");
                pd_loading.setVisibility(View.GONE);
                dialog.show();
                Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();
            }

完整代码

mywebsite = (WebView)findViewById(R.id.mywebsite);
        pd_loading = (ProgressBar)findViewById(R.id.pd_loading);
        CallWebView();


        mywebsite.setWebViewClient(new WebViewClient());
        mywebsite.loadUrl(Information.URL_Home);

        mywebsite.setWebViewClient(new WebViewClient(){

            public void onPageFinished(WebView view ,String url)
            {
                pd_loading.setVisibility(View.GONE);
                mywebsite.setVisibility(View.VISIBLE);
            }

            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                mywebsite.loadUrl("about:blank");
                pd_loading.setVisibility(View.GONE);
                dialog.show();
                Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();
            }




        });

当网络连接丢失时,旧设备无法看到onReceivedError,但新设备没有问题

1 个答案:

答案 0 :(得分:1)

添加方法:

  

onReceivedError(WebView视图,int errorCode,字符串描述,   String failingUrl)

此方法用于API级别22 - 。

 mywebsite.setWebViewClient(new WebViewClient(){

            public void onPageFinished(WebView view ,String url)
            {
                pd_loading.setVisibility(View.GONE);
                mywebsite.setVisibility(View.VISIBLE);
            }

            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                mywebsite.loadUrl("about:blank");
                pd_loading.setVisibility(View.GONE);
                dialog.show();
                Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();
            }


           //For versions < Android M (API 23).
           @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                 mywebsite.loadUrl("about:blank");
                pd_loading.setVisibility(View.GONE);
                dialog.show();
                Toasty.error(Home.this,"Failed",Toast.LENGTH_SHORT,true).show();

            }


        });