android - 为什么Android WebView显示黑屏?

标签 android webview

今天下午我一直在用头撞墙试图让 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,它只会在模拟器中显示黑屏。最后我想从本地文件中读取。

包含在 list 标签下,XML 架构与 Hello Webview 示例相同。

我在这里遗漏了什么明显的东西吗? :(

最佳答案

尝试改变

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>

关于android - 为什么Android WebView显示黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1981507/

相关文章:

android - 我可以将 Android 4 HttpResponseCache 与基于 WebView 的应用程序一起使用吗?

android - 在android中的 ScrollView 中加载具有大内容的webview

java - 自上次重启以来的时间 : how can i convert?

android - 创建通知以自行启动非 MAIN Activity ..可能吗?

android - Android 上的 Facebook 不使用 fragment

android - WebView 无法正确渲染,而是显示源代码

android - 未触发嵌套的 RecyclerView onClick

java - 微调器的背景图像出现拉伸(stretch)

android - webview 从 assets 文件夹加载 html 页面

javascript - 从单独的类调用 Activity 函数