2014-05-04 30 views
-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 

回答

1

length=6; index=6

数组0为主。 如果阵列有6个元素,该范围是0,...,5

[编辑]

在你的onClick,i的值增加太多。
它达到一个超过数组长度的值(6)。

您应该添加一个条件来验证i> 5然后将其重置为0.
或者添加一个类似的逻辑(我没有输入您的游戏逻辑)。

只是为了确保我在范围0,...,5内