开发者

Android webview loaddata giving NullPointerException

I have used a webview named Testview to load html data in the webview. for that I am using the following code.

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

I have given <uses-permission android:name="andr开发者_开发技巧oid.permission.INTERNET" /> in the manifest. But this above loine of code is generating NullPointerException. Can anyone point out the problem in my code?


As @m0s pointed in comment: make sure Textview is initialized:

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

Also, it is a Java custom to write class names with first letter capitalized (WebView) and instances with first letter in lower-case (textview), so that they are easily distinguished.

Update:

If this line returns null:

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

then you most probably forgot to call:

setContentView(R.layout.main);

in your activity.onCreate() method. The javadoc of findViewById(int) says:

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

That's what setContentView() does (processes the layout XML):

Set the activity content from a layout resource. The resource will be inflated,
adding all top-level views to the activity.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜