以编程方式设置ImageView Drawable

问题描述 投票:0回答:2

我试图从一系列drawables以编程方式设置ImageView的图像但仍然得到一个NUllPointerException ....这个方法看起来正确设置图像资源..

//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);
android imageview
2个回答
3
投票

您正在将图像资源设置为随机数。你需要这样做:

int theImage = imageSelection[whichImage.nextInt(imageSelection.length)];
displayImage.setBackgroundResource(theImage);

0
投票

试试这个:imageView.setImageResource(R.drawable.image);

© www.soinside.com 2019 - 2024. All rights reserved.