簡體   English   中英

Android WebView顯示黑屏

[英]Android WebView shows black screen

我正在為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>

如果你嘗試這個怎么樣

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM