tomericco tomericco - 1 month ago 17
Android Question

WebViewClient shows white screen

I'm new to Android app development.
I'm managing to show a WebView and load a given URL. When I click on a link in the WebView, I get a blank white screen.

When I use the Chrome browser on the device (Galaxy TAB), it's working. Actually i'm trying to imitate Chrome in my WebView.

Does anyone know what's the problem?

This is the WebViewClient I use in my WebView:

siteView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {

siteView.loadUrl(urlNewString);
return true;
}

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

super.onPageStarted(view, url, favicon);

if (dialog == null || !dialog.isShowing()) {

if(isFirstTime) {

dialog = ProgressDialog.show(MyActivity.this, "", getString(R.string.loadingMessage), true, false);
MyActivity.isFirstTime = false;
}
}
}

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

Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});

Answer

The problem may lie in your shouldOverrideUrlLoading function. Your are receiving "view" as a parameter and you using "siteView" to load url. Your function should look like:

public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {

        view.loadUrl(urlNewString); // you are using siteView here instead of view
        return true;
    } 

Hope this works for you.

Comments