开发者

Getting a drawable by integer in a hashmap

开发者 https://www.devze.com 2023-03-07 07:41 出处:网络
I\'m trying to display a different drawable for each day but I\'m getting a NullPointerException. have been at开发者_高级运维 this for hours now.

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);


Glad you figured it out :)

Quicker and simpler option for setting the background, by the way, would be this:

ivDay.setImageResource(dayMap.get("day" + mDay));

Saves the call to getResources() and getDrawable() since you already have the ID.

0

精彩评论

暂无评论...
验证码 换一张
取 消