为什么Android WebView 显示黑屏? [英] Why does Android WebView display a black screen?

查看:113
本文介绍了为什么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>

这篇关于为什么Android WebView 显示黑屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆