我在下面的代码中有一个 arrayoutofindexexception:
ImageView[] bObject = new ImageView[9];
private void getCorrectObject() {
List<Integer> objects = new ArrayList<Integer>();
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
objects.add(5);
objects.add(6);
objects.add(7);
objects.add(8);
objects.add(9);
Collections.shuffle(objects);
int correctObject = objects.get(0);
Log.d("test", String.valueOf(correctObject));
bObject[correctObject + 1].setImageResource(R.drawable.stage1_4_object1);
bObject[correctObject + 1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
goToNextQuestion();
}
});
}
//TODO initControls
private void initControls() {
bObject[0] = (ImageView) findViewById(R.id.bObject1Stage1_4);
bObject[0].setImageResource(R.drawable.stage1_4_object2);
bObject[1] = (ImageView) findViewById(R.id.bObject2Stage1_4);
bObject[1].setImageResource(R.drawable.stage1_4_object2);
bObject[2] = (ImageView) findViewById(R.id.bObject3Stage1_4);
bObject[2].setImageResource(R.drawable.stage1_4_object2);
bObject[3] = (ImageView) findViewById(R.id.bObject4Stage1_4);
bObject[3].setImageResource(R.drawable.stage1_4_object2);
bObject[4] = (ImageView) findViewById(R.id.bObject5Stage1_4);
bObject[4].setImageResource(R.drawable.stage1_4_object2);
bObject[5] = (ImageView) findViewById(R.id.bObject6Stage1_4);
bObject[5].setImageResource(R.drawable.stage1_4_object2);
bObject[6] = (ImageView) findViewById(R.id.bObject7Stage1_4);
bObject[6].setImageResource(R.drawable.stage1_4_object2);
bObject[7] = (ImageView) findViewById(R.id.bObject8Stage1_4);
bObject[7].setImageResource(R.drawable.stage1_4_object2);
bObject[8] = (ImageView) findViewById(R.id.bObject9Stage1_4);
bObject[8].setImageResource(R.drawable.stage1_4_object2);
for (ImageView bObj : bObject) {
bObj.setOnClickListener(this);
}
我在这一行遇到错误:
bObject[correctObject + 1].setImageResource(R.drawable.stage1_4_object1);
我不知道这有什么问题。如果您发现问题所在,我们将不胜感激任何帮助。谢谢