简体   繁体   中英

Android WebView shows black screen

I'm trying this example for the Android WebView:

http://www.linuxtopia.org/online_books/android/devguide/guide/tutorials/views/hello-webview.html

but I don't get what I want, here are my files:

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

   private WebView webview;

   /**
    * Called when the activity is first created.
    */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      webview = (WebView) findViewById(R.id.webview);
      webview.setWebViewClient(new WebClient());
      webview.getSettings().setJavaScriptEnabled(true);
      webview.loadUrl("http://www.google.com");
   }

   private class WebClient extends WebViewClient {

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;

      }
   }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.sheya"
          android:versionCode="1"
          android:versionName="1.0">
   <uses-permission android:name="android.permission.INTERNET" />
   <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
      <activity android:name="MainActivity"
                  android:label="@string/app_name">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

The fact is, when I launch my application, it shows a black screen, but if I remove the

webview.setWebViewClient(new WebClient());

line from application code, it opens the google page into the default browser...

-- EDIT --

Solved it, in the main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" <!-- Instead of wrap_content -->
    android:layout_height="fill_parent" <!-- Instead of wrap_content -->
    android:orientation="vertical">

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

How about if you try this

@Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return super.shouldOverrideUrlLoading(view, url);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM