android:WebView,显示assets文件夹中的html内容

时间:2013-07-31 11:08:45

标签: android xml webview

帮帮我。我试图在android中显示我的html页面。但它显示错误。

我在xml中的代码,

    <WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical" />

与java一样;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView wv;  
    wv = (WebView) findViewById(R.id.webview);  
    wv.loadUrl("file:///android_asset/chapter1.html");  
}

}

但是,在运行应用程序时......它显示的错误如下;

07-31 11:04:05.056: D/AndroidRuntime(1008): Shutting down VM
07-31 11:04:05.096: W/dalvikvm(1008): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-31 11:04:05.136: E/AndroidRuntime(1008): FATAL EXCEPTION: main
07-31 11:04:05.136: E/AndroidRuntime(1008): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thaufeeq.tamilquran/com.thaufeeq.tamilquran.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.os.Looper.loop(Looper.java:137)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at dalvik.system.NativeStart.main(Native Method)
07-31 11:04:05.136: E/AndroidRuntime(1008): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.Activity.setContentView(Activity.java:1881)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at com.thaufeeq.tamilquran.MainActivity.onCreate(MainActivity.java:13)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.Activity.performCreate(Activity.java:5104)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-31 11:04:05.136: E/AndroidRuntime(1008):     ... 11 more
07-31 11:04:05.136: E/AndroidRuntime(1008): Caused by: java.lang.reflect.InvocationTargetException
07-31 11:04:05.136: E/AndroidRuntime(1008):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
07-31 11:04:05.136: E/AndroidRuntime(1008):     ... 23 more
07-31 11:04:05.136: E/AndroidRuntime(1008): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060001 a=-1 r=0x7f060001}
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.content.res.Resources.loadDrawable(Resources.java:1927)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.View.<init>(View.java:3330)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.View.<init>(View.java:3259)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.view.ViewGroup.<init>(ViewGroup.java:425)
07-31 11:04:05.136: E/AndroidRuntime(1008):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
07-31 11:04:05.136: E/AndroidRuntime(1008):     ... 26 more

2 个答案:

答案 0 :(得分:1)

你的权利。当我查看时,此代码适用于我。更好的是,您再次使用此代码进行调试,替换下面的代码。 (没有更多修改)

你的JAVA文件:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class Webview extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        WebView wv;  
        wv = (WebView) findViewById(R.id.webview);  
        wv.loadUrl("file:///android_asset/chapter1.html");  
    }   
}

您的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Webview" >

<WebView android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:id="@+id/webview">
 </WebView>

</RelativeLayout>

答案 1 :(得分:0)

我也遇到了同样的问题所以我将该文件从资产复制到了SD卡。 它解决了我的问题。 如果可能,请查看一次AssetManager类

private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File f = new File(Controller.getController()
                    .getApplicationBasePath() + "/Document_Directory");
            if (f.exists()) {
                out = new FileOutputStream(Controller.getController()
                        .getApplicationBasePath()
                        + "/Document_Directory/"
                        + filename);

                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } else {
                f.mkdir();
                copyAssets();
            }
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}