0

I am trying to set the image of a ImageView programmatically from a array of drawables but keep getting a NUllPointerException....does this method look correct for setting Image resource..

//get a random drawable
int[] imageSelection= {R.drawable.buddycheck, R.drawable.logdive3, R.drawable.sea, R.drawable.weather, R.drawable.logo1};
Random whichImage = new Random();
int theImage = whichImage.nextInt(imageSelection.length);

displayImage.setBackgroundResource(theImage);
1
  • 1
    Where and how do you initialize displayImage ? Commented Feb 23, 2014 at 16:13

2 Answers 2

4

You are setting the image resource to a random number. You need to do it like this:

int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this : imageView.setImageResource(R.drawable.image);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.