为什么Android WebView会显示黑屏?

为什么Android WebView会显示黑屏?,android,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(savedInstan

今天下午我一直在努力让网络视图正常工作。以下是主类中的代码:

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/");

    }
}
public类扩展活动{
/**在首次创建活动时调用*/
网络视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(webview)findviewbyd(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
//工作
//webview.loadUrl(“http://www.google.com");
//不起作用
//webview.loadUrl(“http://www.theregister.co.uk");
//loadData(“hello”、“text/html”、“utf-8”);
//loadDataWithBaseURL(“假:/”、“你好”、“文本/html”、“utf-8”、”http://www.theregister.co.uk/");
}
}
这是Google的“你好,Webview”示例。如果我使用WebView并尝试访问www.google.com,那么它可以正常工作。如果我尝试访问任何其他站点,那么它会失败,包括loadData,并且它只会在emulator中显示一个黑屏。最后,我想从本地文件中读取

包含在manifest标记下,XML模式与Hello Webview示例相同


我是不是漏掉了什么明显的东西(

尝试
UTF-8
而不是
UTF-8
进行后两次尝试。我可以使用相同的代码加载—尝试将其加载到内置浏览器应用程序中,如果失败,您可能会遇到某种防火墙/代理问题

从我的一本书中演示WebView的简单用法。

尝试更改

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

在main.xml顶级线性布局中

应该是这样的:

<?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.permission.INTERNET使用权限造成的。

这是针对最新SDK和Google API的:)你试过对setJavaScriptEnabled的调用进行注释吗?当黑屏出现时,
adb logcat
会说什么?我遇到了与海报相同的问题,这为我解决了问题。谢谢泰勒!您的意思是说布局参数对webview可以加载或不加载的URL有任何影响?根据:此值从API级别8开始被弃用,并由MATCH_PARENT替换
<?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>