myc_100 2012-09-11 06:53 采纳率: 10%
浏览 9195
已采纳

为什么Android的网页视图会黑屏显示?

我一直在尝试让网页视图能够正常运行,但是还是不行。下面是主类的代码:

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,程序可以运行。如果我想连接别的网站,数据加载就会失败,并且以黑屏显示。最后我只能从本机文件中读出数据。我在程序中缺少什么了吗?

  • 写回答

7条回答 默认 最新

  • Curie-87 2012-09-11 07:21
    关注

    把主页面顶端线型布局

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

    改为

    android:layout_width="fill_parent"
    android:layout_height="fill_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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备