Webview从资产目录加载HTML


142

我正在尝试从资产目录加载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);  
    }  
}

我在LogCat中并没有发现任何明显的错误...


看到我的其他答案:stackoverflow.com/a/8694428/341091
马丁

Answers:


295

在设置“内容”视图之前,您正在获取WebView,因此wv可能为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
        }  
    }

就是这样 我以这种方式开始,尝试交换它,但是现在它可以工作了……很酷。
AndyD273 2010年

顺便说一句,黑白色是Android的标准外观吗?我所有的表格视图默认情况下都是黑底白字,但是我的html设置为黑底白字...我可以更改它们,但不确定要更改哪一个。
AndyD273 2010年

在我的网站的移动contactus page.html页面中包含一个电子邮件地址,该用户使用android应用程序中的webview和seturl打开该用户的weburl contactus.html页面,单击电子邮件未知的URL模式错误
Harsha

如何在Android应用程序中使用服务器运行HTML FILE?

16

每当创建活动时,都必须setcontentview在超级调用之后添加(布局)。因为setcontentview将xml绑定到您的活动中,所以这就是您获得的原因nullpointerexception

 setContentView(R.layout.webview);  
 webView = (WebView) findViewById(R.id.webView1);
 wv.loadUrl("file:///android_asset/xyz.html");

6
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

2
在任何情况下,除非我的资产文件夹位于错误的文件夹中,否则Andoid doc都引用 assets developer.android.com/tools/projects/index.html,除非该文件夹当前位于main
Pawel Cioch 2015年

我的坏我有同一个项目的不同势位置的副本,并添加到错误的副本,但这个答案与图片是最好的stackoverflow.com/questions/18302603/...
帕维尔Cioch

0

从此处下载源代码(从资产android打开html文件

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();
 }
 }
 });
 }
 }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.