ArrayIndexOutOfBoundsException Ошибка в приложении викторины YoungLearnerActivity - PullRequest
0 голосов
/ 08 сентября 2018

Я создаю викторину для Android с четырьмя действиями. Я столкнулся с некоторой проблемой. Когда я запускаю приложение, отвечаю на вопросы и нажимаю кнопку «Далее», текущие вопросы сохраняют все данные в каждом массиве. Но когда я заканчиваю тест и нажимаю кнопку «Получить результат», приложение останавливается.

В Run Box показывает эту ошибку

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.enkoala.enkoala, PID: 5060
              java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
                  at com.enkoala.enkoala.YoungLearnerActivity$1.onClick(YoungLearnerActivity.java:84)
                  at android.view.View.performClick(View.java:5703)
                  at android.view.View$PerformClick.run(View.java:22808)
                  at android.os.Handler.handleCallback(Handler.java:836)
                  at android.os.Handler.dispatchMessage(Handler.java:103)
                  at android.os.Looper.loop(Looper.java:203)
                  at android.app.ActivityThread.main(ActivityThread.java:6293)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)

YoungLearnerActivity

package com.enkoala.enkoala;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;

public class YoungLearnerActivity extends AppCompatActivity {

public static final String EXTRA_SCORE="extra_score";

private ImageView imageView;
private TextView textViewQuestion;
private TextView textViewQuestionCount;
private RadioGroup radioGroup;
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private Button buttonNext;

private List<YoungLearnerQuestion> youngLearnerQuestionList;

private int questionCounter;
private int questionCountTotal;
private YoungLearnerQuestion currentQuestion;

private int score;
private boolean answered;

int[] listed_image;
String[] listed_question;
String[] listed_option1;
String[] listed_option2;
String[] listed_option3;
int[] listed_answernumber;
int[] user_answers;
boolean [] true_false;

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

    imageView = findViewById(R.id.image_view_y);
    textViewQuestion= findViewById(R.id.text_view_question_y);
    textViewQuestionCount= findViewById(R.id.text_view_question_count_y);
    radioGroup= findViewById(R.id.radio_group_y);
    radioButton1= findViewById(R.id.radio_button1_y);
    radioButton2= findViewById(R.id.radio_button2_y);
    radioButton3= findViewById(R.id.radio_button3_y);
    buttonNext= findViewById(R.id.button_next_y);

    YoungLearnerDatabaseHelper youngLearnerDatabaseHelper = new YoungLearnerDatabaseHelper(this);
    youngLearnerQuestionList = youngLearnerDatabaseHelper.getAllYoungLearnerQuestion();

    listed_image = new int[youngLearnerQuestionList.size()];
    listed_question = new String[youngLearnerQuestionList.size()];
    listed_option1 = new String[youngLearnerQuestionList.size()];
    listed_option2 = new String[youngLearnerQuestionList.size()];
    listed_option3 = new String[youngLearnerQuestionList.size()];
    listed_answernumber = new int[youngLearnerQuestionList.size()];
    user_answers = new int[youngLearnerQuestionList.size()];
    true_false = new boolean [youngLearnerQuestionList.size()];

    questionCountTotal= youngLearnerQuestionList.size() ;
    Collections.shuffle(youngLearnerQuestionList);
    showNextQuestion();

    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!answered){
                if (radioButton1.isChecked() || radioButton2.isChecked() || radioButton3.isChecked()){
                    RadioButton radioButtonselected=findViewById(radioGroup.getCheckedRadioButtonId());
                    listed_image[questionCounter]= currentQuestion.getYimage();
                    listed_question[questionCounter]=currentQuestion.getYquestion();
                    listed_option1[questionCounter]=currentQuestion.getYoption1();
                    listed_option2[questionCounter]=currentQuestion.getYoption2();
                    listed_option3[questionCounter]=currentQuestion.getYoption3();
                    listed_answernumber[questionCounter]=currentQuestion.getYanswernumber();
                    user_answers[questionCounter]= radioGroup.indexOfChild(radioButtonselected) + 1;
                    checkAnswer();
                }else {
                    Toast.makeText(YoungLearnerActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
                }
            }else {
                showNextQuestion();

            }
        }
    });
}
private void showNextQuestion(){
    radioGroup.clearCheck();

    if (questionCounter<questionCountTotal){
        currentQuestion=youngLearnerQuestionList.get(questionCounter);
        imageView.setImageResource(currentQuestion.getYimage());
        textViewQuestion.setText(currentQuestion.getYquestion());
        radioButton1.setText(currentQuestion.getYoption1());
        radioButton2.setText(currentQuestion.getYoption2());
        radioButton3.setText(currentQuestion.getYoption3());

        questionCounter++;
        textViewQuestionCount.setText("Question: "+ questionCounter + "/" + questionCountTotal);
        answered=false;
        buttonNext.setText("Next");
    }else {
        buttonNext.setText("Get Result");
        showResult();
    }
}
public void showResult(){
    Intent resultIntent = new Intent(YoungLearnerActivity.this,YoungLearnerResultActivity.class);
    resultIntent.putExtra(EXTRA_SCORE,score);
    resultIntent.putExtra("listed_image" , listed_image);
    resultIntent.putExtra("listed_question",listed_question);
    resultIntent.putExtra("listed_option1",listed_option1);
    resultIntent.putExtra("listed_option2",listed_option2);
    resultIntent.putExtra("listed_option3",listed_option3);
    resultIntent.putExtra("listed_answernumber",listed_answernumber);
    resultIntent.putExtra("user_answer",user_answers);
    resultIntent.putExtra("true_false",true_false);
    startActivity(resultIntent);
}
private void checkAnswer(){
    answered=true;
    RadioButton radioButtonselected=findViewById(radioGroup.getCheckedRadioButtonId());
    int answernumber = radioGroup.indexOfChild(radioButtonselected) + 1;
    if (answernumber == currentQuestion.getYanswernumber()){
        true_false [questionCounter] = true;
        score++;
    }
    showNextQuestion();
}  }
...