Android WebView显示黑屏

时间:2012-12-04 20:23:38

标签: android web-applications browser webview default

我正在为Android WebView尝试此示例:

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

但我没有得到我想要的东西,这是我的文件:

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>

事实是,当我启动我的应用程序时,它显示黑屏,但如果我删除

webview.setWebViewClient(new WebClient());

从应用程序代码开始,它将google页面打开到默认浏览器...

- 编辑 -

main.xml 布局文件中解决了它:

<?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>

1 个答案:

答案 0 :(得分:0)

如果你试试这个怎么样

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