字符串数组随机化ArrayIndexOutOfBoundsException(已编辑)

时间:2013-12-17 03:36:18

标签: android arrays string indexoutofboundsexception

需要帮助的人。我在我的代码中添加了随机方法,现在我收到了这个错误。


12-17 11:03:38.921: E/AndroidRuntime(306): FATAL EXCEPTION: main
12-17 11:03:38.921: E/AndroidRuntime(306): java.lang.ArrayIndexOutOfBoundsException
12-17 11:03:38.921: E/AndroidRuntime(306):  at com.example.boardrace.MainActivity.isCorrect(MainActivity.java:97)
12-17 11:03:38.921: E/AndroidRuntime(306):  at com.example.boardrace.MainActivity.checkAnswer(MainActivity.java:107)
12-17 11:03:38.921: E/AndroidRuntime(306):  at com.example.boardrace.MainActivity$1.onClick(MainActivity.java:48)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.view.View.performClick(View.java:2408)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.view.View$PerformClick.run(View.java:8816)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.os.Handler.handleCallback(Handler.java:587)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.os.Looper.loop(Looper.java:123)
12-17 11:03:38.921: E/AndroidRuntime(306):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-17 11:03:38.921: E/AndroidRuntime(306):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 11:03:38.921: E/AndroidRuntime(306):  at java.lang.reflect.Method.invoke(Method.java:521)
12-17 11:03:38.921: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-17 11:03:38.921: E/AndroidRuntime(306):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-17 11:03:38.921: E/AndroidRuntime(306):  at dalvik.system.NativeStart.main(Native Method)

这是我在youtube中看到的整个代码

public class MainActivity extends Activity {

private int currentQuestion;
private String [] questions;
private String [] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText; 

Random random = new Random();
String randString = questions[random.nextInt(questions.length)];

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

public void init()
{questions = new String[]{"Q1?","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"};
 answers = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8","A9","10",};
 currentQuestion = -1;
 answerButton = (Button)findViewById(R.id.AnswerButton);
 questionButton = (Button)findViewById(R.id.QuestionButton);
 questionView = (TextView) findViewById(R.id.QuestionTextView);
 answerView = (TextView) findViewById(R.id.AnswerTextView);
 answerText = (EditText) findViewById(R.id.AnswerText);

 answerButton.setOnClickListener(new OnClickListener(){
 @Override
 public void onClick(View v) {checkAnswer();              }});

 questionButton.setOnClickListener(new OnClickListener(){
     @Override
 public void onClick(View v) {
 showQuestion();
     }});
}

public void showQuestion()
{ currentQuestion++;
 if(currentQuestion == questions.length)
 currentQuestion =0;
questionView.setText(questions[currentQuestion]);
 answerView.setText("");
 answerText.setText("");
}

public boolean isCorrect(String answer)
{return (answer.equalsIgnoreCase(answers[currentQuestion]));
}

public void checkAnswer()
{String answer = answerText.getText().toString();
 if(isCorrect(answer))
 answerView.setText("You're right!");
 else answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}
}

代码是关于问题的随机化,您将输入正确的答案。

4 个答案:

答案 0 :(得分:0)

试试这个:

questions[random.nextInt(questions.length-1)];

这个想法是阵列位置从0开始。所以你的问题数组的值在0到9之间。

你传递的总长度为10,超出了最后一个位置,即9。

答案 1 :(得分:0)

尝试删除数组

末尾的逗号“,”
answers = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8","A9","10",};

作为

answers = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8","A9","10"};

答案 2 :(得分:0)

问题是你初始化currentQuestion = -1;。因此,如果您第一次点击答案按钮而不点击之前的问题按钮,它将会执行:

public boolean isCorrect(String answer) {
     return (answer.equalsIgnoreCase(answers[-1]));
}

因此IndexOutOfBoundsException

答案 3 :(得分:0)

String randString = questions[random.nextInt(questions.length)];

由于randString被声明为成员变量,所以这是在创建类的对象时赋值,此赋值取决于questions,它在init()中初始化,这在创建对象后将调用init()。这就是您获得ArrayIndexOutOfBoundException的原因,因为您在初始化之前尝试访问questions

流速:

  1. 创建了对象。
  2. 会员已初始化。

    2.a)randString已分配值,但这需要question

    2.b)现在,查找question

    2.c)只能找到question的声明。 question并不是指任何价值。

    2.d)现在questions[random.nextInt(questions.length)]正在尝试访问某个索引处的值(random int),但question到目前为止还没有任何内容。

    This is point where exception is raised.

  3. Solution

    如果randString是某个初始化参数,那么执行

    init(){
    // some code
    questions = new String[]{"Q1?","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"};
    String randString = questions[random.nextInt(questions.length)];
    // some code
    }