为什么Android WebView 显示黑屏?
今天下午我一直在用头撞墙,试图让 WebView 工作.下面是主类中的代码:
I've been banging my head against a wall this afternoon trying to get a WebView to work. Below is the code in the main class:
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,它只会在模拟器中显示黑屏.最后我想从本地文件中读取.
This is Google's "Hello, Webview" example. If I use a WebView and try to access www.google.com then it works fine. If I try to access any other site then it fails including loadData and it just displays a black screen in the emulator. In the end I would like to read from a local file.
包含在清单标记下,XML 架构与 Hello Webview 示例相同.
is included under the manifest tag and the XML schema is the same as the Hello Webview example.
我在这里遗漏了什么明显的东西吗?:(
Am I missing something obvious here? :(
尝试改变
android:layout_width="wrap_content"
android:layout_height="wrap_content"
到
android:layout_width="fill_parent"
android:layout_height="fill_parent"
在你的 main.xml 顶级 LinearLayout 中
in your main.xml top level 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>