Home » Android » java – (android studio) can't change the "Webpage not available" page in webview-Exceptionshub

java – (android studio) can't change the "Webpage not available" page in webview-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m trying to display my own “no internet” page in my app.
I think something might be wrong with my code, I’ve been following this tutorial but for some reason, my code keeps displaying the default “Webpage not available” screen.

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webView.loadUrl("https://www.kasumushi.com/");

        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                webView.loadUrl("file:///android_asset/error.html");

            }
        });
    }

    @Override
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        }else {
            super.onBackPressed();
        }
    }

}
How to&Answers:
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();
    }
});

Answer:

would you please check the below procedure and try to retest your problem.

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "Error! " + description, Toast.LENGTH_SHORT).show();
                    //Clear WebView
                    try {
                        view.stopLoading();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        view.clearView();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (view.canGoBack()) {
                        view.goBack();
                    }
                    String ErrorPagePath = "file:///android_asset/htmls/error.html";
                    view.loadUrl(ErrorPagePath);

                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
            });