今天下午我一直在撞牆,試圖讓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示例相同。
我在這裏錯過了一些明顯的東西嗎? :(
這是針對最新的SDK和Google API :) – Biz 2009-12-30 18:01:29
您是否嘗試過註釋掉setJavaScriptEnabled調用? – CaseyB 2009-12-30 18:17:24
當黑屏出現時,adb logcat會說些什麼? – 2009-12-30 18:49:00