2009-12-30 324 views
5

今天下午我一直在撞牆,試圖讓WebView工作。以下是主類中的代碼:爲什麼Android WebView顯示黑屏?

public class fkyougoogle extends Activity { 
    /** Called when the activity is first created. */ 
WebView webview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webview = (WebView) findViewById(R.id.webview); 
     webview.getSettings().setJavaScriptEnabled(true); 
     // WORKS 
     //webview.loadUrl("http://www.google.com"); 
     // DOESN'T WORK 
     //webview.loadUrl("http://www.theregister.co.uk"); 
     //webview.loadData("<html><body>hello</body></html>", "text/html", "utf-8"); 
     //webview.loadDataWithBaseURL("fake://", "<html><body>hello</body></html>", "text/html", "utf-8", "http://www.theregister.co.uk/"); 

    } 
} 

這是Google的「Hello,Webview」示例。如果我使用WebView並嘗試訪問www.google.com,那麼它工作正常。如果我嘗試訪問任何其他網站,那麼它會失敗,包括loadData,它只是在模擬器中顯示一個黑屏。最後我想從本地文件中讀取。

包含在清單標記下,XML模式與Hello Webview示例相同。

我在這裏錯過了一些明顯的東西嗎? :(

+0

這是針對最新的SDK和Google API :) – Biz 2009-12-30 18:01:29

+0

您是否嘗試過註釋掉setJavaScriptEnabled調用? – CaseyB 2009-12-30 18:17:24

+0

當黑屏出現時,adb logcat會說些什麼? – 2009-12-30 18:49:00

回答

0

嘗試UTF-8,而不是utf-8你後兩次嘗試我沒有問題裝載http://www.theregister.co.uk使用相同的代碼 - 嘗試加載它內置的瀏覽器應用程序,如果失敗,你也許遇到某種形式的防火牆/代理問題。

Here are a few projects展示的WebView的簡單應用,從我的書之一。

7

嘗試改變

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
在main.xml中最高級別的LinearLayout

它應該是這樣的:

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

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

</LinearLayout> 
+0

我和海報有同樣的問題,這爲我修好了。謝謝泰勒! – 2010-01-11 18:40:40

+1

你的意思是說佈局參數對webview可以載入或不載入什麼url有影響? – 2010-10-23 14:28:15

+0

據: [ViewGroup.LayoutParams FILL_PARENT](http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#FILL_PARENT) 此值已被棄用在API級別8開始和替換由MATCH_PARENT提供 – 2014-02-05 14:46:03

0

我有一個WebView中的類似問題是完全空白,但我的情況下,它是引起由一個缺少android.permission.INTERNET使用權限。