Web view onReceivedError is handled but still showing web page not available

后端 未结 2 546
攒了一身酷
攒了一身酷 2021-01-28 21:08

I was trying to show a web page on my web view. and in case if an error occurs or page loading fails I want to show a sad smiley and a text showing that your page is not loaded

相关标签:
2条回答
  • 2021-01-28 21:52

    Because onReceivedError called before onPageFinished, so your sadSmiley and errorText gone.

    Try below code,

    boolean errorOccurred = false; // Global variable
    
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            hideError();
            showProgress();
            Toast.makeText(Test.this, "start loading", Toast.LENGTH_SHORT).show();
            errorOccurred=false;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!errorOccurred) {
                hideError();
            }
            hideProgress();
            Toast.makeText(Test.this, "Web view was loaded", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            errorOccurred = true;
            hideProgress();
            showError();
            Toast.makeText(Test.this, "Could not load your page", Toast.LENGTH_SHORT).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
            Toast.makeText(Test.this, "error", Toast.LENGTH_SHORT).show();
        }
    });
    
    0 讨论(0)
  • 2021-01-28 22:00

    Move the line super.onReceivedError(view,errorCode,description,failingUrl); above hideProgress(); in onReceivedError() method.

    0 讨论(0)
提交回复
热议问题