【问题标题】:Webview load html from assets directoryWebview 从资产目录加载 html
【发布时间】:2011-03-10 07:22:40
【问题描述】:

我正在尝试从资产目录加载一个 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 中并没有发现任何明显的错误...

【问题讨论】:

标签: android


【解决方案1】:

您在设置内容视图之前获取 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
        }  
    }

【讨论】:

  • 就是这样。我一开始就是这样,尝试交换它,但现在它可以工作了......酷。
  • 顺便说一句,黑底白字是 Android 的标准外观吗?默认情况下,我所有的表格视图都是黑底白字,但我的 html 设置为白底黑字...我可以更改它们,但不确定要更改哪一个。
  • 在我的网站移动contactus page.html页面中包含一个电子邮件地址,我在android应用程序和seturl中使用webview打开该用户点击电子邮件未知url架构错误的weburl contactus .html页面
  • 如何在android应用程序中使用服务器运行HTML文件?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

    【解决方案4】:

    从这里下载源代码 (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();
     }
     }
     });
     }
     }
    

    【讨论】:

      猜你喜欢
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 2017-11-04
      • 2020-05-16
      相关资源
      最近更新 更多