vendredi 25 mars 2016

Creating a random quiz with buttons app for android, but crashes if the first questions displays first

I'm trying to create a random quiz app, but i'm facing a particular problem that i was not able to solve in the last 2 days.

I have all my questions and options inside of 2 different array of Strings. An int called flag which determines the progress of the quiz

int flag=0; 

String allQuestions[]={"A","B","C","D","E","F","G","H","I","J"};

String allOptions[]={/*/Primeras opciones /*/"A","I","O",
                     /*/Segundas opciones/*/"E","O","I",
                     /*/Terceras opciones/*/"A","I","U",
                      /*/Cuartas opciones/*/"O","E","U",
                       */Quintas opciones/*/"O","U","A",
                            /*/Sextas opciones/*/"I","U","E",
                            /*/Septimas opciones/*/"E","I","E"
                            /*/Octavas opciones/*/"A","I","U",
                            /*/Novenas opciones/*/"E","U","O",
                            /*/Decimas opciones/*/"A","O","U"};

This is how i pick the random numbers:

ArrayList<Integer> number = new ArrayList<Integer>();
    for (int i = 1; i <= 10; ++i) number.add(i);
    Collections.shuffle(number);

I have my buttons linked and make their text change once the quiz activity is created.

question=(TextView)findViewById(R.id.QuizCharacter);
    answerA=(Button)findViewById(R.id.answerA);
    answerB=(Button)findViewById(R.id.answerB);
    answerC=(Button)findViewById(R.id.answerC);
    answerD=(Button)findViewById(R.id.answerD);
    answerE=(Button)findViewById(R.id.answerE);
    answerF=(Button)findViewById(R.id.answerF);

    question.setText(number.get(flag));     // <------ having problems here?
    answerA.setText(allOptions[0]);
    answerB.setText(allOptions[1]);
    answerC.setText(allOptions[2]);
    answerD.setText(allOptions[3]);
    answerE.setText(allOptions[4]);
    answerF.setText(allOptions[5]);

Now, the randomizer works, the problem is that if the created random number is the first question A, then the app crashes. I tried changing what i assume is the problematic line for

question.setText(allQuestions[number.get(0)]);

And instead of having a chance to randomize, it crashes every time.

Why is it happening and how can i fix it?




Aucun commentaire:

Enregistrer un commentaire