0

I'm making an Android quiz app and it seems like it's all done except adding the questions but there is still a big error. You see I've put that the random generater generates 4 questions. When I answer the fourth I would like that a new activity starts with the display of the score. Instead the app just doesn't respond. I'm really clueless.

Thank you!

package com.matej.hajdukkviz;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Glavno extends Activity implements OnClickListener {

int score  = 0;

TextView textView1, textView2, textView3, countdown;
Button btn1, btn2, btn3, btn4;

ArrayList<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();

ArrayList<String> allAnswers = new ArrayList<String>();

Random rng = new Random();
Question nextQuestion;

Question q1 = new Question(
        "Q1",

        "Correct answer - q1",
        "Wrong answer 1 - q1",
        "Wrong answer 2 - q1",
        "Wrong answer 3 - q1"
        );
Question q2 = new Question(
        "Q2?",

        "Correct answer - q2",
        "Wrong answer 1 - q2",
        "Wrong answer 2 - q2",
        "Wrong answer 3 - q2"
        );
Question q3 = new Question(
        "Q3?",

        "Correct answer - q3"
        "Wrong answer 1 - q3",
        "Wrong answer 2 - q3",
        "Wrong answer 3 - q3"
        );

Question q4 = new Question(
        "Q4?",

        "Correct answer - q4",
        "Wrong answer 1 - q4",
        "Wrong answer 2 - q4",
        "Wrong answer 3 - q4"
        );

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.pitanja);

// ADD THE QUESTIONS IN THE ArrayList qsts

    qsts.add(q1);           
    qsts.add(q2);
    qsts.add(q3);
    qsts.add(q4);

    textView1 = (TextView) findViewById(R.id.textView1);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);
    countdown = (TextView) findViewById(R.id.countdown);

    textView3.setText("Rezultat: " + score);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

    generateQuestion();

}

    public void generateQuestion(){

        while(true){

            int nxt = rng.nextInt(4);

            if (!generated.contains(nxt)){

                generated.add(nxt);

                nextQuestion = qsts.get(nxt);

                textView1.setText(nextQuestion.questionText);

                allAnswers.add(nextQuestion.correctAnswerText);
                allAnswers.add(nextQuestion.wrongAnswer1);
                allAnswers.add(nextQuestion.wrongAnswer2);
                allAnswers.add(nextQuestion.wrongAnswer3);

                Collections.shuffle(allAnswers);

                btn1.setText(allAnswers.get(0));
                btn2.setText(allAnswers.get(1));
                btn3.setText(allAnswers.get(2));
                btn4.setText(allAnswers.get(3));

                break;
            }
        }
    }


    @Override
    public void onClick(View v) {
        Button b = (Button)v;
        String buttonText = b.getText().toString();

        if(buttonText.equals(nextQuestion.correctAnswerText)) { 

            textView2.setText("TOČNO!");
            textView2.setTextColor(Color.GREEN);
            textView3.setText("Rezultat: " + (score += 10));

            allAnswers.clear();
            generateQuestion();

            return;

        }else{

            textView2.setText("NETOČNO!");
            textView2.setTextColor(Color.RED);
            textView3.setText("Rezultat: " + (score -= 5));

            allAnswers.clear();
            generateQuestion();

            return; 
        }

    }   

}

Jo Ke
  • 145
  • 1
  • 2
  • 12
  • Post your logcat so we can see where the error is. Also, where are you trying to start a new `Activity`? – codeMagic Jul 29 '13 at 19:11
  • there is no red text in the logcat it just doesnt respond... in this activity – Jo Ke Jul 29 '13 at 19:12
  • Did you make sure the filter in your logcat is set to either "verbose" or "error"? Then step through and see what line causes it to stop responding. Also, I still don't see where you try to start a new `Activity`... – codeMagic Jul 29 '13 at 19:13
  • I don't know where to start it. You see that there is an OnClick method that tells what to do after the button is clicked. It sets a new question etc. But I would like that after the last question it takes me to a new activity.. – Jo Ke Jul 29 '13 at 19:16
  • Have you created this new `Activity` yet? – codeMagic Jul 29 '13 at 19:18
  • yes I did. It's empty, – Jo Ke Jul 29 '13 at 19:20
  • just create the intent to the new activity and pass as parameter the score. http://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to-another – Tobiel Jul 29 '13 at 21:16

2 Answers2

0

Put some stuff in your next Activity, make sure it is properly declared in the manifest.xml, create a layout for it then add this code after your last question is answered

  Intent intent = new Intent(Glavno.this, NextActivityName.class);   // declare and initialize an Intent object
  startActivity(intent); // pass the Intent object to startActivity()

Intents

Activities

Edit

I see you already have accepted an answer but it sounds like its not working correctly. You aren't finishing so the code in your onClick() runs through. I'll edit what you have and post below. That should help.

@Override
public void onClick(View v) {
    Button b = (Button)v;
    String buttonText = b.getText().toString();

    if (counter == 4) {
        startActivity(new Intent(Glavno.this, Score.class));
        finish();   // Added this method call
    }

    else if(buttonText.equals(nextQuestion.correctAnswerText)) { // Changed this to else if

        counter++;

        AdView ad = (AdView) findViewById(R.id.ads);
        ad.loadAd(new AdRequest());

        textView2.setText("TOČNO!");
        textView2.setTextColor(Color.GREEN);
        textView3.setText("Rezultat: " + (score += 10));

        allAnswers.clear();
        generateQuestion();

        return;
    } 

    else{

        counter++;

        AdView ad = (AdView) findViewById(R.id.ads);
        ad.loadAd(new AdRequest());

        textView2.setText("NETOČNO!");
        textView2.setTextColor(Color.RED);
        textView3.setText("Rezultat: " + (score -= 5));

        allAnswers.clear();
        generateQuestion();

        return; 
    }

}   
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • but how do I put this to start after the last questions is answered? – Jo Ke Jul 29 '13 at 19:26
  • Create an `int` counter and add 1 each time they click the button. In `onClick()` check if counter equals question list size. If it does, start the next `Activity` – codeMagic Jul 29 '13 at 19:28
0

In your generateQuestion() your are looping while true (which is always the case) and then execute something only if the random question has not been shown. Of course this is an endless loop. If it is 4 questions, you can declare a global variable counter which you increase after each question. And instead of looping while true you say if counter < 4 else startNewIntent() or similar with the code mentioned by codeMagic.

Christian
  • 4,596
  • 1
  • 26
  • 33
  • I've added a int counter that start with zero and increases with every click on a button.. I've added the startActivity(new Intent()) in the on click above these methods but it doesn't work.. – Jo Ke Jul 29 '13 at 23:55
  • did you start the Activity with the code mentioned by codeMagic? Do you have the second Activity called for example `NextActivityName`? If all yes, you have to post your updated code to see where the error is still. – Christian Jul 30 '13 at 07:35