Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.
The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.
You are getting the WebView before setting the Content view so the wv is probably null.
public class ViewWeb extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}
Whenever you are creating activity, you must add setcontentview
(your layout) after super call. Because setcontentview
bind xml into your activity so that's the reason you are getting nullpointerexception
.
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/xyz.html");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wb = new WebView(this);
wb.loadUrl("file:///android_asset/index.html");
setContentView(wb);
}
keep your .html in `asset` folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With