1
我試圖創建一個數組,然後生成上ImageView的隨機圖像上ImageView的隨機圖像,但我的代碼有問題... setBackgroundResource
生成錯誤和消息的Android工作室Cannot resolve method 'setBackgroundResource(int)'
我的代碼是:生成與陣列
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.btn);
final RelativeLayout background = (RelativeLayout) findViewById(R.id.back);
Resources res = getResources();
final TypedArray myImages = res.obtainTypedArray(R.array.myImages);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Random random = new Random();
int randomInt = random.nextInt(myImages.length());
int drawableID = myImages.getResourceId(randomInt, -1);
background.setBackgroundResource(drawableID);
}
});
}