Saturday, 23 July 2016

android - Webview load html from assets directory



I'm trying to load a html page from the assets directory. I tried this, but it fails.



public class ViewWeb extends Activity {  
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // fails here
setContentView(R.layout.webview);
}
}


I don't really get any telling errors in LogCat...


Answer




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

}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...