【问题标题】:Android - Null pointer calling Arraylist with getter/setter.Android - 使用 getter/setter 调用 Arraylist 的空指针。
【发布时间】:2013-07-23 16:44:49
【问题描述】:

我正在尝试跟踪 Steam URL 播放的历史记录,以及 getCurrentPosition 的值,以使用户能够在该会话期间中断的地方继续播放视频。我的策略是使用两个 Arraylist 来存储数据,比较 URL 并通过 Intent Extras 将先前的位置填回去。

我在调用我的 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);
            }

我确定我忽略了一些愚蠢的事情,如果我错过了什么,我将不胜感激。提前谢谢你。

【问题讨论】:

  • 你初始化historyList了吗?

标签: android arraylist


【解决方案1】:
WatchedGS historyList;

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

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

【讨论】:

  • 对不起,是的,我将历史列表初始化为:historyList = new WatchedGS();在 oncreate 中。
  • 实际上,比尔,你是对的,一旦我将 init 移到我正在调用 getURL 的事件中,它就可以正常工作。谢谢,我现在可以睡觉了。 :)
  • @Chad 不客气。我很高兴我能提供一组相对新鲜的眼睛。 :)
猜你喜欢
  • 1970-01-01
  • 2021-01-05
  • 2022-08-16
  • 2015-04-09
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多