通过哈希图中的整数获取可绘制对象
我试图每天显示不同的可绘制对象,但出现 NullPointerException。 已经在这几个小时了。
private Map<String, Integer> dayMap;
...
dayMap = new HashMap<String, Integer>();
dayMap.put("day1", R.drawable.day1);
dayMap.put("day2", R.drawable.day2);
dayMap.put("day3", R.drawable.day3);
dayMap.put("day4", R.drawable.day4);
dayMap.put("day5", R.drawable.day5);
...
int mDay = 2; //set as 2 for testing
...
ivDay.setImageDrawable(getResources().getDrawable(dayMap.get("day" + String.valueOf(mDay)))); //NullPointerException!!!
-编辑-
哇,我现在感觉真的很愚蠢,但是发布我的问题帮助我回答了它...必须告诉它 ivday 应该是什么....抱歉占用您的时间:(
ivDay = (ImageView)findViewById(R.id.imageView1);
I'm trying to display a different drawable for each day but I'm getting a NullPointerException.
have been at this for hours now.
private Map<String, Integer> dayMap;
...
dayMap = new HashMap<String, Integer>();
dayMap.put("day1", R.drawable.day1);
dayMap.put("day2", R.drawable.day2);
dayMap.put("day3", R.drawable.day3);
dayMap.put("day4", R.drawable.day4);
dayMap.put("day5", R.drawable.day5);
...
int mDay = 2; //set as 2 for testing
...
ivDay.setImageDrawable(getResources().getDrawable(dayMap.get("day" + String.valueOf(mDay)))); //NullPointerException!!!
-EDIT-
wow i feel really dumb right now but posting my question helped me answer it... for got to tell it what ivday was supposed to be.... sorry for your time :(
ivDay = (ImageView)findViewById(R.id.imageView1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很高兴你明白了:) 顺便
说一句,设置背景的更快、更简单的选项是这样的:
保存对 getResources() 和 getDrawable() 的调用,因为你已经有了 ID。
Glad you figured it out :)
Quicker and simpler option for setting the background, by the way, would be this:
Saves the call to getResources() and getDrawable() since you already have the ID.