-3
您好我是新來的andoid並不斷在我的logcat中出現以下錯誤,但我無法找到我的Java代碼中的問題。應用程序在遍歷數組兩次後崩潰。我不確定數組在哪裏出界?數組越界錯誤android
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