Android WebView LoadData Dare NullPointerException
-
15-11-2019 - |
Domanda
Ho usato una visualizzazione Web Vista Web denominata Testview
per caricare i dati HTML in WebView.Per questo sto usando il seguente codice.
Testview.loadData("<html><body>helloindia</body></html>", "text/html", "utf-8");
Ho dato <uses-permission android:name="android.permission.INTERNET" />
nel manifest.Ma questo sopra la locazione del codice sta generando NullPointerException
.Qualcuno può indicare il problema nel mio codice?
Soluzione
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.