2017-06-21 43 views
-1

这是我的代码:的Android工作室活动不开,当我把下面的代码段

package com.appsoft23.bestmathtrainerforkids.free; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.Random; 

public class Add extends AppCompatActivity { 

    Button startButton; 
    ArrayList<Integer> answers = new ArrayList<Integer>(); 
    int correctLocation; 

    public void start(View view){ 

     startButton.setVisibility(View.INVISIBLE); 
    } 

     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add); 

     startButton = (Button) findViewById(R.id.startButton); 
     TextView addTextView = (TextView) findViewById(R.id.addTextView); 
     Button button4 = (Button) findViewById(R.id.button4); 
     Button button5 = (Button) findViewById(R.id.button5); 
     Button button6 = (Button) findViewById(R.id.button6); 
     Button button7 = (Button) findViewById(R.id.button7); 


     Random rand = new Random(); 
     int h = rand.nextInt(21); 
     int t = rand.nextInt(21); 
     addTextView.setText(Integer.toString(h) + "+" + Integer.toString(t)); 
     correctLocation = rand.nextInt(4); 
     int wrongAnswer; 
     for(int i=0; i<4; i++){ 
      if (i == correctLocation){ 
       answers.add(h+t); 
      } else { 
       wrongAnswer = rand.nextInt(42); 
       while (wrongAnswer == h+t){ 
        wrongAnswer = rand.nextInt(42); 
       } 
       answers.add(wrongAnswer); 
      } 
     } 
     button4.setText(Integer.toString(answers.get(4))); 
     button5.setText(Integer.toString(answers.get(5))); 
     button6.setText(Integer.toString(answers.get(6))); 
     button7.setText(Integer.toString(answers.get(7))); 

    } 


} 

它提供了以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.appsoft23.bestmathtrainerforkids.free, PID: 3095 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appsoft23.bestmathtrainerforkids.free/com.appsoft23.bestmathtrainerforkids.free.Add}: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.get(ArrayList.java:411) at com.appsoft23.bestmathtrainerforkids.free.Add.onCreate(Add.java:53) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

请帮助:)谢谢

回答

1

你的阵列列表给出IndexOutOfBoundsException。因为数组列表只包含4个元素,所以你从5个位置调用它。 更改:

button4.setText(Integer.toString(answers.get(0))); 
    button5.setText(Integer.toString(answers.get(1))); 
    button6.setText(Integer.toString(answers.get(2))); 
    button7.setText(Integer.toString(answers.get(3))); 
+0

我正在错误,谢谢你纠正:) –

+0

Np个:)请接受我的答案 –

0

它trows例外,因为你的数组大小为4,您尝试检索从该位置不数组中存在 试试这个

button4.setText(Integer.toString(answers.get(0))); 
button5.setText(Integer.toString(answers.get(1))); 
button6.setText(Integer.toString(answers.get(2))); 
button7.setText(Integer.toString(answers.get(3))); 
0

要添加数据到arraylist使用for循环4次。所以大小是4,索引是0,1,2,3。

的按钮,你正在使用的索引4,5,6,7

设置从ArrayList中的文本,因此厚望