gpt4 book ai didi

android - webview 从 assets 文件夹加载 html 页面

转载 作者:行者123 更新时间:2023-11-29 17:50:22 36 4
gpt4 key购买 nike

因此,我尝试显示 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)

我已经尝试了很多不同的东西,但似乎不明白这个错误。任何帮助将不胜感激。在这些东西上还是新手。

最佳答案

您的 WebView 是错误的,这就是您得到 NullPointerException 的原因。应该是

wv = (WebView) findViewById(R.id.webview);

关于android - webview 从 assets 文件夹加载 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311644/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com