Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening new android activity with a webview shows a blank (white) screen instead of the html content

I have been researching for 2 days and I haven't found a similar problem anywhere on the internet!

From the main activity, I'm trying to open a new activity containing a webview that will show an html page (about.html) in the android_asset folder.

I'm able to launch the new activity (test.java) with the webview properly displayed but the webpage content just doesn't show up. Even the webview's vertical scrollbar is displayed (as the page content is relatively long) and I can scroll the page up and down but with no content in it!

package com.example.test;  import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView;  public class test extends Activity { private TextView apppagetitle; private WebView browser_rn;  @Override public void onCreate(Bundle saveInstanceState) {     super.onCreate(saveInstanceState);      setContentView(R.layout.test);      browser_rn=(WebView)findViewById(R.id.webkit);     browser_rn.getSettings().setJavaScriptEnabled(true);     browser_rn.getSettings().setPluginsEnabled(true);     browser_rn.setScrollBarStyle(0);             final AlertDialog alertDialog = new AlertDialog.Builder(this).create();     apppagetitle=(TextView)findViewById(R.id.apppagetitle);       browser_rn.setWebViewClient(new WebViewClient() {          public boolean shouldOverrideUrlLoading(WebView view, String url) {             view.loadUrl(url);             return false;         }          public void onPageFinished(WebView view, String url) {                            apppagetitle.setText(browser_rn.getTitle());                         }          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {             if (!isInternetOn()) {                 alertDialog.setTitle("Connection Error");                 alertDialog.setMessage("You need to be connected to the internet.");             } else {                 alertDialog.setTitle("Error");                 alertDialog.setMessage(description);             }             alertDialog.setButton("OK", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int which) {                     return;                 }             });             alertDialog.show();         }     });      browser_rn.loadUrl("file:///android_asset/about.html"); }  public boolean onKeyDown(int keyCode, KeyEvent event)  {     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {         browser_rn.clearCache(true);         browser_rn.clearHistory();         this.finish();         return true;     }     return super.onKeyDown(keyCode, event); } } 

The weird thing is that the new activity (test.java) can successfully get and display the webpage title in the textview when I call webview.gettitle() but the html content just doesn't render in the webview. All I see is a blank screen with scrollbars. I suspect it is the boolean "shouldOverrideUrlLoading" that might be causing it.

Update:

Thanks for replying.

test.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/com.example.test" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout   android:orientation="horizontal"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:background="@drawable/green" > <TextView    android:id="@+id/apppagetitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:text="Odd News" android:textSize="25px" android:padding="10px" />  </LinearLayout> <LinearLayout   android:orientation="vertical"   android:layout_width="fill_parent"   android:layout_height="fill_parent"> <WebView   android:id="@+id/webkit" android:layout_width="fill_parent"  android:layout_height="0px" android:layout_weight="1" />  </LinearLayout> </LinearLayout> 

about.html is as simple as what is below.

<!DOCTYPE html>  <html>  <head>  <meta charset="utf-8" />  <title>About</title>  </head>  <body>  <h3 style="color:#0066dd">About</h3>    <p>Some text here...</p>  </body>  </html> 

Oh yes, and I forgot to mention earlier that with the same piece of code, the about.html would sometimes show up if I force-stop and restart the app on my phone. However, it would then not appear in subsequent launches. On occasions when it successfully shows up about.html and I press the back button to return to the main activity, it will disappear again when I re-launch the activity from the main activity. I'm lost but I have a strong feeling it is "shouldOverrideUrlLoading" that I'm not doing correctly.

like image 671
Lost Avatar asked Jan 02 '11 08:01

Lost


1 Answers

Don't try to load the given url in shouldOverrideUrlLoading, and don't return true from it in your case either. Really, from what it looks like you're trying to do, you shouldn't need to override it at all.

Returning true from shouldOverrideUrlLoading indicates to the WebView that it should not load the specified URL. What you're doing - telling the WebView to begin loading it again - is likely to cause some sort of internal loop inside the WebView, which I'd guess is why you're seeing odd behavior.

like image 112
tophyr Avatar answered Oct 06 '22 07:10

tophyr