0

我是 Android 开发的新手,无法在这里解决问题。我创建了一个名为 video 的空白类,具有两个属性名称和 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 的活动中添加视频,当我将视频添加到列表时会引发错误:

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
4

1 回答 1

1

您需要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
于 2013-02-06T13:19:03.670 回答