我实际上想要一个textView改变当一组图片匹配的背景图像的按钮数组。我不知道为什么应用程序在打开时崩溃,因为下面的代码。下面是代码:
protected void checkResult(int bx2[],int demon[]) {
// TODO Auto-generated method stub
final int[] cards1={R.drawable.one1,R.drawable.one2,
R.drawable.one3,R.drawable.two1,R.drawable.two2,
R.drawable.two3,R.drawable.three1,
R.drawable.three2,0};
int klop=0;
for(int i=0; i<9;i++) {
int sene = cards1[i];
Button vl = (Button) findViewById(bx2[i]);
if(vl.getBackground()==getResources().getDrawable(sene)) {
klop+=1;
if(klop==7) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("PUZZLE SOLVED!");
}
}
}
}`
你的代码将崩溃与ResourceNotFoundException
时,你试图获得Drawable时,i = 8
和sene = cards1[8] = 0;
,以避免崩溃,你可以添加一个简单的检查
int sene = cards1[i];
if (sene == 0)
continue;