从资产目录中的WebView加载HTML加载、资产、目录中、WebView

2023-09-11 12:44:34 作者:陈獨秀

我试图从资产目录中加载HTML页面。我想这一点,但它失败。

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);  
    }  
}

我真的没有得到任何有说服力的错误在LogCat中...

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

推荐答案

您所得到的web视图设置内容视图以便WV可能是空了。

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