webview从assets文件夹加载html页面

时间:2014-04-26 13:33:52

标签: android webview

所以我试图从assets文件夹中显示html页面,但不知怎的,我总是得到同样的错误。 这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

这是我的java文件:

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

public class GroundFloor extends Activity {

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

这就是我得到的错误:

04-26 14:30:41.306: E/AndroidRuntime(32009): FATAL EXCEPTION: main
04-26 14:30:41.306: E/AndroidRuntime(32009): Process: com.example.mmu, PID: 32009
04-26 14:30:41.306: E/AndroidRuntime(32009): java.lang.RuntimeException: Unable to         start activity ComponentInfo{com.example.mmu/com.example.mmu.GroundFloor}:  java.lang.NullPointerException
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread.access$800(ActivityThread.java:157)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.os.Looper.loop(Looper.java:157)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread.main(ActivityThread.java:5293)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at java.lang.reflect.Method.invokeNative(Native Method)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at java.lang.reflect.Method.invoke(Method.java:515)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at dalvik.system.NativeStart.main(Native Method)
04-26 14:30:41.306: E/AndroidRuntime(32009): Caused by: java.lang.NullPointerException
04-26 14:30:41.306: E/AndroidRuntime(32009):    at com.example.mmu.GroundFloor.onCreate(GroundFloor.java:15)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.Activity.performCreate(Activity.java:5389)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-26 14:30:41.306: E/AndroidRuntime(32009):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)

我尝试了很多不同的东西,但似乎并不理解错误。任何帮助将不胜感激。相当新手还在那些东西上。

1 个答案:

答案 0 :(得分:3)

您的网络视图有误,这就是您获得NullPointerException的原因。它应该是

wv = (WebView) findViewById(R.id.webview);
相关问题