我从我的hashmap收到一个空值。这是创建的hashmap:
private HashMap<String,Bitmap> thumbs = new HashMap<String,Bitmap>();
/* adding a single value to the hashmap */
然后我继续从hashmap中检索值,如下所示:
public Bitmap getImageByFileName(String fileName) {
Bitmap fish = null;
Iterator it = thumbs.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
fish = (Bitmap)thumbs.get(fileName);
it.remove();
}
Log.i("shnitzel", " bitmap is " + fish);
fish = (Bitmap)thumbs.get(fileName);
Log.i("shnitzel", " final bitmap is " + fish);
return fish;
}
日志文件:
08-05 22:18:28.170: I/shnitzel(477): bitmap is android.graphics.Bitmap@40650138
08-05 22:18:28.170: I/shnitzel(477): final bitmap is null
正如您所看到的,我在'while'循环内外使用完全相同的命令,但由于某种原因,它在其中工作,但不在外部。为什么会这样?
答案 0 :(得分:3)
在你的循环中:
it.remove();
您在阅读后删除该元素。
答案 1 :(得分:1)