Android-使用getter/setter调用Arraylist的Null指针



我正在尝试跟踪播放的steam URL的历史记录,以及getCurrentPosition的值,以便为用户提供恢复他们在会话期间中断的视频的能力。我的策略是使用两个Arraylist来存储数据,比较URL,并通过intent extra将上一个位置填充回来。

我在给我的getter打电话时收到了一个NPE。以下是重要部分:

Getter/Setter类:

public class WatchedGS {
private ArrayList videoURL = new ArrayList();
private ArrayList resumeTime = new ArrayList();
public ArrayList getURL() {
return videoURL;
}
public void setURL(String videoURL) {
    this.videoURL.add(videoURL);
    Log.i("videoURL added: ", videoURL);
}
public ArrayList getresumeTime() {
    return resumeTime;
}
public void setresumeTime(int time) {
    this.resumeTime.add(time);
    Log.i("videoTime added: ", ""+ time);
}
}

媒体播放器呼叫设置在表面销毁,确认工作:

    public void surfaceDestroyed(SurfaceHolder holder) {
    watchedGS.setURL(urlString);
    watchedGS.setresumeTime(player.getCurrentPosition());
    player.release();
    player = null;
    finish();
}

MainActivity调用get,导致NPE:

    WatchedGS historyList;
            if (videoHistory){
            int alSize = historyList.getURL().size();
            if (historyList.getURL().contains(url)) {
                Log.i("it's there!", url);
            }

我确信我忽略了一些愚蠢的东西,如果我错过了什么,我将不胜感激。提前谢谢。

WatchedGS historyList;
        if (videoHistory){
        int alSize = historyList.getURL().size();
        if (historyList.getURL().contains(url)) {
            Log.i("it's there!", url);
        }

在调用getURL()之前,您没有初始化historyList

最新更新