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

时间:2013-07-23 16:44:49

标签: android arraylist

我正在尝试跟踪播放的Steam网址的历史记录,以及getCurrentPosition的值,以便用户能够恢复在该会话期间停止播放的视频。我的策略是使用两个Arraylists来存储数据,比较URL并将之前的位置填充到intent ads中。

我在打电话给我的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);
            }

我确信我已经忽略了一些愚蠢的事情,并会欣赏我错过的任何指示。提前谢谢。

1 个答案:

答案 0 :(得分:1)

WatchedGS historyList;

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

在调用historyList之前,您尚未初始化getURL()