1

帮我。我试图在 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
4

2 回答 2

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>
于 2013-08-05T06:29:52.713 回答
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);
        }
    }
}
于 2013-07-31T11:14:02.327 回答