<分区>
我使用以下代码从 Android Activity 中的 LinkedList 中获取项目
currentAlarm = alarmQueue.peek();
alarmQueue 本身就是这样创建的,在一个扩展 Application 的类中
public static Queue<Long> getAlarmQueue() {
if (alarmQueue == null)
{
Log.d(TAG, "alarmqueue was null, creating new one");
alarmQueue = new LinkedList<Long>();
}
return alarmQueue;
}
该应用程序处于生产状态并已在 google play 上发布,但有时会出现错误报告,指出包含 .peek() 的行抛出 nullpointerexception。因此我添加了您在上面看到的空检查,因为我认为 alarmQueue 本身是空的。 实际上,经过一些测试,我意识到如果 LinkedList 不包含任何项目,.peek() 会抛出 nullpointerexception。
我明白,它可能会返回 null,但文档并没有说它会抛出异常 Android Doc LinkedList
我在这里错过了什么? 谢谢!
编辑:异常图像证明 EDIT2:我找到了原因.. 多么可怕的错误...... currentAlarm 的类型是 long 而不是 Long,所以 nullpointerexception 被引发了 long 是原始的数据类型,不能将 null 作为参数:/ 但是,这个错误应该永远不会发生,因为在这一点上,队列内部必须有一些东西,所以我必须继续深入挖掘。但是对于这个错误,一切都很好,并添加了错误处理。