您好我是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.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
答案 0 :(得分:1)
length=6; index=6
数组基于0。 如果您的数组有6个元素,则范围为0,...,5
<强> [编辑] 强>
在你的onClick中,i的值增加太多 它到达的值(6)超过了数组长度。
您应添加条件以验证我是否&gt; 5然后将其重置为0 或者添加一个类似的逻辑(我没有输入你游戏的逻辑)。
只是为了确保我在0,...,5的范围内。