0

尝试在活动中创建 Web 视图时遇到一个非常奇怪的问题。

我按照 Android WebView 指南执行此操作(http://developer.android.com/guide/webapps/webview.html)

我的应用程序有一个主菜单,上面有一个按钮,可以引导一个应该打开 Web 视图的活动。当我使用 Intent 时它工作得很好,但我需要更多的灵活性,所以这不是一个真正的选择。

这是我的 webview.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</WebView>

这是我的 WebView 活动文件中的内容:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webview = (WebView) findViewById(R.id.webview);
    diningWebView.loadUrl(http://www.google.com);
}

问题是我的 findViewById() 返回 null,但我不知道为什么。

有任何想法吗?

4

2 回答 2

4

你忘了你的 setContentView()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView( R.layout.webview );   

    WebView webview = (WebView) findViewById(R.id.webview);
    diningWebView.loadUrl(http://www.google.com);
}
于 2012-08-08T23:12:17.227 回答
0

作为记录,还有一个可能的原因:有几种不同的布局,例如纵向和横向,但并非所有布局都定义了 WebView。

于 2013-09-18T13:11:43.340 回答