1

我使用了一个名为的 webviewTestview在 webview 中加载 html 数据。为此,我正在使用以下代码。

Testview.loadData("<html><body>helloindia</body></html>", "text/html", "utf-8");

我已经<uses-permission android:name="android.permission.INTERNET" />在清单中给出了。但是上面这段代码正在生成NullPointerException。谁能指出我的代码中的问题?

4

1 回答 1

3

正如@m0s 在评论中指出的那样:确保 Textview已初始化

textview = new WebView(this);  // used inside an Activity

此外,Java 的习惯是编写首字母大写的类名(WebView)和首字母小写的实例(textview),以便于区分。

更新:

如果此行返回 null:

Textview = (WebView)this.findViewById(R.id.testview)

那么你很可能忘记打电话了:

setContentView(R.layout.main);

在你的activity.onCreate()方法中。findViewById(int) 的 javadoc 说:

Finds a view that was identified by the id attribute from the XML THAT WAS 
PROCESSED in onCreate(Bundle).

这就是setContentView()(处理布局 XML):

Set the activity content from a layout resource. The resource will be inflated,
adding all top-level views to the activity.
于 2011-02-19T09:31:51.503 回答