Android Webview 从资产目录加载 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3152422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webview load html from assets directory
提问by AndyD273
I'm trying to load a html page from the assets directory. I tried this, but it fails.
我正在尝试从资产目录加载一个 html 页面。我试过这个,但它失败了。
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...
我真的没有在 LogCat 中得到任何明显的错误...
回答by Robby Pond
You are getting the WebView before setting the Content view so the wv is probably null.
您在设置内容视图之前获取 WebView,因此 wv 可能为空。
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
}
}
回答by duggu
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
在超级调用之后添加(您的布局)。因为setcontentview
将 xml 绑定到您的活动中,这就是您获得nullpointerexception
.
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/xyz.html");
回答by BIJU KV
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
回答by Deepshikha Puri
Download source code from here (Open html file from assets android)
从这里下载源代码(从 assets android 打开 html 文件)
activity_main.xml
活动_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
主活动.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();
}
}
});
}
}