Search code examples
androidwebview

Why can't I get onPageStarted, onReceivedError, or onPageFinished to ever call with Android WebView?


I want to be able to recognize when an error occurs and tell show a toast message with the error but I can never get them to call. I also want to be able to tell when the webview starts navigating to a url and detect which url it's going to.

Unfortunately it just seems to ignore everything and I have no idea how to start a function or method or anything once this occurs.I don't really care how I go about determining when a page starts loading or an error occurs I just need a way that works.

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.webview);

       myWebView.setWebViewClient(new WebViewClient());
       myWebView.setWebChromeClient(new WebChromeClient()
        {

            private boolean error;


            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                onPageStarted(view, url, favicon);
                Toast.makeText(getApplicationContext(), "page started:" + url, Toast.LENGTH_SHORT).show();
                error = false;
            }


            public void onReceivedError( WebView view, int errorCode, String description, String failingUrl)  {

                error = true;
                System.out.println("description error" + description);
                view.setVisibility( View.GONE );
                Toast.makeText(getApplicationContext(), "error:" + description, Toast.LENGTH_SHORT).show();
            }


            public void onPageFinished(WebView view, String url) {

                if (!error) {
                    view.setVisibility( View.VISIBLE );
                }
                error = false;

                Toast.makeText(getApplicationContext(), "page finished" + url, Toast.LENGTH_SHORT).show();
            }
        }
        );





        myWebView.setBackgroundColor(0);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setAllowFileAccess(true);


        // Other webview settings
        myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");

        myWebView.loadUrl("https://example.com/");



    }

}

Solution

  • The onPageStarted(), onPageFinished(), and onReceivedError() methods are not defined in the WebChromeClient class. They're members of WebViewClient. You subclassed the wrong one.

    If you add the @Override annotation above each method, your IDE should yell at you that those methods don't override anything in the super class, which is why that annotation is useful.

    Simply move those overrides to the WebViewClient instance.

    myWebView.setWebViewClient(new WebViewClient() {
            private boolean error;
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                ...
            }
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                ...
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                ...
            }
        }
    );
    
    myWebView.setWebChromeClient(new WebChromeClient());