161

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...

AndyD273
  • 7,177
  • 12
  • 54
  • 92

4 Answers4

321

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
        }  
    }
Samuel
  • 9,883
  • 5
  • 45
  • 57
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • That was it. I had it that way to begin with, tried swapping it, but now it works... Cool. – AndyD273 Jun 30 '10 at 19:03
  • On a side note, is the white on black a standard look in Android? All my table views are white on black by default, but my html is set for black on white... I can change them, but not sure which one to change. – AndyD273 Jun 30 '10 at 19:06
  • in my website mobile contactus page.html page contains one email address is there,am using webview in android app and seturl to open that weburl contactus .html page in that user click on email unknown url schema error – Harsha Oct 25 '16 at 06:57
  • How to run HTML FILE using the server in android application? –  May 03 '17 at 05:40
17

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");
duggu
  • 37,851
  • 12
  • 116
  • 113
7
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
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
BIJU KV
  • 89
  • 1
  • 2
  • 2
    Andoid doc refers to `assets` https://developer.android.com/tools/projects/index.html in either case does not work, unless my asset(s) folder is in wrong folder currently in `main` – Pawel Cioch Mar 05 '15 at 19:01
  • My bad I had a copy of same project in diffrent location, and was adding to wrong copy, but this answer with image is the best http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio – Pawel Cioch Mar 05 '15 at 19:10
1

Download source code from here (Open html file from assets android)

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:background="#FFFFFF"
 android:layout_height="match_parent">

<WebView
 android:layout_width="match_parent"
 android:id="@+id/webview"
 android:layout_height="match_parent"
 android:layout_margin="10dp"></WebView>
</RelativeLayout>

MainActivity.java

package com.deepshikha.htmlfromassets;
 import android.app.ProgressDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

WebView webview;
 ProgressDialog progressDialog;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 init();
 }

private void init(){
 webview = (WebView)findViewById(R.id.webview);
 webview.loadUrl("file:///android_asset/download.html");
 webview.requestFocus();

progressDialog = new ProgressDialog(MainActivity.this);
 progressDialog.setMessage("Loading");
 progressDialog.setCancelable(false);
 progressDialog.show();

webview.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
 try {
 progressDialog.dismiss();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
 }
 }
Deepshikha Puri
  • 2,104
  • 22
  • 23