gpt4 book ai didi

java - 将模型类添加到通用列表会引发错误

转载 作者:行者123 更新时间:2023-12-01 14:52:11 26 4
gpt4 key购买 nike

我是 Android 开发新手,无法解决这里的问题。我创建了一个名为 video 的空白类,它具有两个属性 name 和 url:

public class Video {
public String _name;
public String _Url;

public Video(String name, String Url)
{
_name = name;
_Url = Url;
}
public String getName()
{
return _name;
}
public String getUrl()
{
return _Url;
}

}

然后,我有一个通用列表,用于在调用 VideosListActivity 的 Activity 中添加视频,当我将视频添加到列表中时,会引发错误:

public List<Video> ListResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Update view

setContentView(R.layout.videos);

setTitle("VIDEOS");

//--videos created here
Video NewVideo = new Video("video one","http://www.youtube.com/watch?v=cxLG2wtE7TM");

ListResult.add(NewVideo);

Log.v("VideoList", "Opened list");

抛出这个错误:

02-06 13:10:05.660: E/AndroidRuntime(23432): FATAL EXCEPTION: main
02-06 13:10:05.660: E/AndroidRuntime(23432): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.mosquitodigital.panic/uk.co.mosquitodigital.panic.VideoListActivity}: java.lang.NullPointerException
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.access$600(ActivityThread.java:139)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.os.Looper.loop(Looper.java:156)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.main(ActivityThread.java:5045)
02-06 13:10:05.660: E/AndroidRuntime(23432): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 13:10:05.660: E/AndroidRuntime(23432): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 13:10:05.660: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-06 13:10:05.660: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-06 13:10:05.660: E/AndroidRuntime(23432): at dalvik.system.NativeStart.main(Native Method)
02-06 13:10:05.660: E/AndroidRuntime(23432): Caused by: java.lang.NullPointerException
02-06 13:10:05.660: E/AndroidRuntime(23432): at uk.co.mosquitodigital.panic.VideoListActivity.onCreate(VideoListActivity.java:31)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.Activity.performCreate(Activity.java:4543)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
02-06 13:10:05.660: E/AndroidRuntime(23432): ... 11 more

最佳答案

您需要先初始化 ListResult 列表,然后再向其中添加元素:

public List<Video> ListResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Update view

setContentView(R.layout.videos);

ListResult= new ArrayList<Video>(); //<< initialize List here

关于java - 将模型类添加到通用列表会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730038/

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