bwoogie
bwoogie

Reputation: 4427

Getting a drawable by integer in a hashmap

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

Upvotes: 0

Views: 3798

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134704

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.

Upvotes: 2

Related Questions