正如@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.