我从 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”循环内部和外部使用完全相同的命令,但由于某种原因它在循环内部起作用,但在外部不起作用。为什么会出现这种情况?
最佳答案
在你的循环中:
it.remove();
您在阅读该元素后将其删除。
关于Java hashmap 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18066100/