Android WebView LoadData는 NullPointerException을 제공합니다
-
15-11-2019 - |
문제
Testview
라는 WebView를 사용하여 WebView의 HTML 데이터를로드했습니다.그 때문에 다음 코드를 사용하고 있습니다.
Testview.loadData("<html><body>helloindia</body></html>", "text/html", "utf-8");
나는 매니페스트에서 <uses-permission android:name="android.permission.INTERNET" />
를주었습니다.그러나이 코드의 위대한 코드는 NullPointerException
를 생성합니다.누구나 내 코드에서 문제를 지적 할 수 있습니까?
해결책
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.