Java 数组越界错误
您好,我是andoid的新手,在我的logcat中不断出现以下错误,但我在java代码中找不到问题。应用程序在数组中循环两次后崩溃。我不确定数组在哪里超出范围 Logcat错误:Java 数组越界错误,java,android,arrays,indexoutofboundsexception,Java,Android,Arrays,Indexoutofboundsexception,您好,我是andoid的新手,在我的logcat中不断出现以下错误,但我在java代码中找不到问题。应用程序在数组中循环两次后崩溃。我不确定数组在哪里超出范围 Logcat错误: 05-04 14:46:22.947 3086-3086/com.example.Finished_app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.Finished_app, PID: 3086 java.lan
05-04 14:46:22.947 3086-3086/com.example.Finished_app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.Finished_app, PID: 3086
java.lang.ArrayIndexOutOfBoundsException: length=6; index=6
at com.example.Finished_app.game1.onClick(game1.java:91)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Java代码:
public class game1 extends Activity implements View.OnClickListener {
static Button next;
static ImageView mainpic;
static RadioGroup radioGroup;
static RadioButton option1;
static RadioButton option2;
static RadioButton option3;
static int[] mapPics = new int[]{R.drawable.america, R.drawable.england, R.drawable.australia, R.drawable.poland, R.drawable.sweden, R.drawable.spain};
static String[] answers = new String[]{"Spain", "Poland", "Sweden", "America", "England", "Australia"};
static int[] correctAnswer = new int[]{2, 1};
static int score = 0;
static int i = 0;
static int a = 0;
static int b = 1;
static int c = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game1);
next = (Button) findViewById(R.id.nextButton);
mainpic = (ImageView) findViewById(R.id.imageView);
mainpic.setImageResource(mapPics[i]);
next.setOnClickListener(this);
addListenerRadioGroup();
option1.setText(String.valueOf(answers[a]));
option2.setText(String.valueOf(answers[b]));
option3.setText(String.valueOf(answers[c]));
}//On Create
public void addListenerRadioGroup() {
option1 = (RadioButton) findViewById(R.id.radioButton);
option2 = (RadioButton) findViewById(R.id.radioButton2);
option3 = (RadioButton) findViewById(R.id.radioButton3);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupAnswers);
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.getCheckedRadioButtonId();
}
});
}
@Override
public void onClick(View v) {
getSelectedAnswer();
i++;
a = a + 3;
b = b + 3;
c = c + 3;
if (i >= 1) {
Intent myIntent = new Intent(this, scores.class);
myIntent.putExtra("scores", score);
startActivity(myIntent);
}//if
mainpic.setImageResource(mapPics[i]);
option1.setText(String.valueOf(answers[a]));
option2.setText(String.valueOf(answers[b]));
option3.setText(String.valueOf(answers[c]));
mapPics[i] = null;
}//onClick
public void getSelectedAnswer() {
int index = radioGroup.indexOfChild(findViewById(radioGroup.getCheckedRadioButtonId()));
if (index == correctAnswer[i])
score++;
}//get selected answer
}//class
length=6;索引=6
数组是基于0的。
如果数组有6个元素,则范围为0、…、5
[编辑]
在您的单击中,i的值增加太多。它到达一个超过数组长度的值(6) 您应该添加一个条件来验证i>5,然后将其重置为0。
或者添加一个类似的逻辑(我不是在输入游戏的逻辑) 只是为了确保我在0,…,5范围内