0

i am working on android quiz. i have 4 buttons for answers and text for these answers retriving from database. if correct answer is choosen i want score incremented and if wrong decremented. i have 20 fix rounds in quiz so whether the answer is wrong or right. i want every time round incremented. when we click answer its show answer right or wrong then proceed to next question.

questionactivity.class

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        /**
         * Configure current game and get question
         */
        currentGame = ((CYKApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn1 = (Button) findViewById(R.id.answer1);
        nextBtn1.setOnClickListener(this);
        Button nextBtn2 = (Button) findViewById(R.id.answer2);
        nextBtn2.setOnClickListener(this);
        Button nextBtn3 = (Button) findViewById(R.id.answer3);
        nextBtn3.setOnClickListener(this);
        Button nextBtn4 = (Button) findViewById(R.id.answer4);
        nextBtn4.setOnClickListener(this);
        /**
         * Update the question and answer options..
         */
        setQuestions();

    }


    /**
     * Method to set the text for the question and answers from the current games
     * current question
     */
    private void setQuestions() {
        //set the question text from current question
        String question = Utility.capitalise(currentQ.getQuestion());
        TextView qText = (TextView) findViewById(R.id.question);
        qText.setText(question);

        //set the available options
        List<String> answers = currentQ.getQuestionOptions();
        TextView option1 = (TextView) findViewById(R.id.answer1);
        option1.setText(Utility.capitalise(answers.get(0)));

        TextView option2 = (TextView) findViewById(R.id.answer2);
        option2.setText(Utility.capitalise(answers.get(1)));

        TextView option3 = (TextView) findViewById(R.id.answer3);
        option3.setText(Utility.capitalise(answers.get(2)));

        TextView option4 = (TextView) findViewById(R.id.answer4);
        option4.setText(Utility.capitalise(answers.get(3)));
    }


    @Override
    public void onClick(View arg0) {
        //Log.d("Questions", "Moving to next question");

        if(!checkAnswer()) return;

        /**
         * check if end of game
         */
        if (currentGame.isGameOver()){
            //Log.d("Questions", "End of game! lets add up the scores..");
            //Log.d("Questions", "Questions Correct: " +            currentGame.getRight());
            //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
            Intent i = new Intent(this, EndgameActivity.class);
            startActivity(i);
            finish();
        }
        else{
            Intent i = new Intent(this, QuestionActivity.class);
            startActivity(i);
            finish();
        }
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK :
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /**
     * Check if a checkbox has been selected, and if it
     * has then check if its correct and update gamescore
     */
    private boolean checkAnswer() {
        String answer = getSelectedAnswer();

            if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                //Log.d("Questions", "Correct Answer!");
                currentGame.incrementScore();
            }
            else{
                //Log.d("Questions", "Incorrect Answer!");
                currentGame.decrementScore();
            }
            return true;
        }

    /**
     * 
     */
    private String getSelectedAnswer() {
        Button c1 = (Button)findViewById(R.id.answer1);
        Button c2 = (Button)findViewById(R.id.answer2);
        Button c3 = (Button)findViewById(R.id.answer3);
        Button c4 = (Button)findViewById(R.id.answer4);
        if (c1.callOnClick())
        {
            return c1.getText().toString();
        }
        if (c2.callOnClick())
        {
            return c2.getText().toString();
        }
        if (c3.callOnClick())
        {
            return c3.getText().toString();
        }
        if (c4.callOnClick())
        {
            return c4.getText().toString();
        }

        return null;
    }

}






gameplay.java  

import java.util.ArrayList;
import java.util.List;


public class GamePlay {

    private int numRounds;
    private int difficulty;
    private int score;
    private int round;

    private List<Question> questions = new ArrayList<Question>();

    /**
     * @return the right
     */
    public int getScore() {
        return score;
    }
    /**
     * @param right the right to set
     */
    public void setScore(int score) {
        this.score = score;
    }

    /**
     * @return the round
     */
    public int getRound() {
        return round;
    }
    /**
     * @param round the round to set
     */
    public void setRound(int round) {
        this.round = round;
    }
    /**
     * @param difficulty the difficulty to set
     */
    public void setDifficulty(int difficulty) {
        this.difficulty = difficulty;
    }
    /**
     * @return the difficulty
     */
    public int getDifficulty() {
        return difficulty;
    }
    /**
     * @param questions the questions to set
     */
    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }

    /**
     * @param q the question to add
     */
    public void addQuestions(Question q) {
        this.questions.add(q);
    }

    /**
     * @return the questions
     */
    public List<Question> getQuestions() {
        return questions;
    }


    public Question getNextQuestion(){

        //get the question
        Question next = questions.get(this.getRound());
        //update the round number to the next round
        this.setRound(this.getRound()+1);
        return next;
    }

    /**
     * method to increment the number of correct answers this game
     */


    /**
     * method to increment the number of incorrect answers this game
     */
    public void incrementScore(){
        score=score+100;
    }

    public void decrementScore()
    {
        score=score-50;
    }
    /**
     * @param numRounds the numRounds to set
     */
    public void setNumRounds(int numRounds) {
        this.numRounds = numRounds;
    }
    /**
     * @return the numRounds
     */
    public int getNumRounds() {
        return numRounds;
    }

    /**
     * method that checks if the game is over
     * @return boolean
     */
    public boolean isGameOver(){
        return (getRound() >= getNumRounds());
    }


}


question.java

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

public class Question {

    private String question;
    private String answer;
    private String option1;
    private String option2;
    private String option3;
    private int rating;


    /**
     * @return the question
     */
    public String getQuestion() {
        return question;
    }
    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;
    }
    /**
     * @return the answer
     */
    public String getAnswer() {
        return answer;
    }
    /**
     * @param answer the answer to set
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    /**
     * @return the rating
     */
    public int getRating() {
        return rating;
    }
    /**
     * @param rating the rating to set
     */
    public void setRating(int rating) {
        this.rating = rating;
    }
    /**
     * @return the option1
     */
    public String getOption1() {
        return option1;
    }
    /**
     * @param option1 the option1 to set
     */
    public void setOption1(String option1) {
        this.option1 = option1;
    }
    /**
     * @return the option2
     */
    public String getOption2() {
        return option2;
    }
    /**
     * @param option2 the option2 to set
     */
    public void setOption2(String option2) {
        this.option2 = option2;
    }
    /**
     * @return the option3
     */
    public String getOption3() {
        return option3;
    }
    /**
     * @param option3 the option3 to set
     */
    public void setOption3(String option3) {
        this.option3 = option3;
    }

    public List<String> getQuestionOptions(){
        List<String> shuffle = new ArrayList<String>();
        shuffle.add(answer);
        shuffle.add(option1);
        shuffle.add(option2);
        shuffle.add(option3);
        Collections.shuffle(shuffle);
        return shuffle;
    }

}

This is my Log File:-

 09-02 08:17:30.083: E/Trace(2148): error opening trace file: No such file or directory (2)
    09-02 08:17:30.193: D/dalvikvm(2148): GC_FOR_ALLOC freed 65K, 8% free 2413K/2620K, paused 26ms, total 28ms
    09-02 08:17:30.203: I/dalvikvm-heap(2148): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
    09-02 08:17:30.313: D/dalvikvm(2148): GC_FOR_ALLOC freed 1K, 5% free 4521K/4732K, paused 104ms, total 104ms
    09-02 08:17:30.363: D/dalvikvm(2148): GC_CONCURRENT freed <1K, 5% free 4521K/4732K, paused 4ms+3ms, total 50ms
    09-02 08:17:30.832: D/gralloc_goldfish(2148): Emulator without GPU emulation detected.
    09-02 08:17:35.432: D/dalvikvm(2148): GC_FOR_ALLOC freed 17K, 4% free 4988K/5148K, paused 42ms, total 45ms
    09-02 08:17:35.482: I/dalvikvm-heap(2148): Grow heap (frag case) to 7.068MB for 2160016-byte allocation
    09-02 08:17:35.562: D/dalvikvm(2148): GC_CONCURRENT freed 4K, 3% free 7092K/7260K, paused 4ms+4ms, total 77ms
    09-02 08:17:36.512: I/Choreographer(2148): Skipped 764 frames!  The application may be doing too much work on its main thread.
    09-02 08:17:38.682: I/dalvikvm(2148): threadid=1: stack overflow on call to Landroid/view/View;.findViewById:LI
    09-02 08:17:38.682: I/dalvikvm(2148):   method requires 12+20+8=40 bytes, fp is 0x4205a320 (32 left)
    09-02 08:17:38.682: I/dalvikvm(2148):   expanding stack end (0x4205a300 to 0x4205a000)
    09-02 08:17:38.682: I/dalvikvm(2148): Shrank stack (to 0x4205a300, curFrame is 0x4205fec4)
    09-02 08:17:38.682: D/AndroidRuntime(2148): Shutting down VM
    09-02 08:17:38.682: W/dalvikvm(2148): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    09-02 08:17:39.092: E/AndroidRuntime(2148): FATAL EXCEPTION: main
    09-02 08:17:39.092: E/AndroidRuntime(2148): java.lang.StackOverflowError
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.Window.findViewById(Window.java:900)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.app.Activity.findViewById(Activity.java:1839)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:144)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
    09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.V
    09-02 08:17:39.122: D/dalvikvm(2148): GC_CONCURRENT freed 762K, 11% free 7907K/8812K, paused 69ms+5ms, total 117ms
    09-02 08:17:39.122: D/dalvikvm(2148): WAIT_FOR_CONCURRENT_GC blocked 16ms
    09-02 08:22:39.222: I/Process(2148): Sending signal. PID: 2148 SIG: 9
    09-02 08:22:39.783: E/Trace(2182): error opening trace file: No such file or directory (2)
    09-02 08:22:39.883: D/dalvikvm(2182): GC_FOR_ALLOC freed 68K, 9% free 2413K/2624K, paused 25ms, total 27ms
    09-02 08:22:39.893: I/dalvikvm-heap(2182): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
    09-02 08:22:40.013: D/dalvikvm(2182): GC_FOR_ALLOC freed 1K, 5% free 4521K/4736K, paused 114ms, total 114ms
    09-02 08:22:40.063: D/dalvikvm(2182): GC_CONCURRENT freed <1K, 5% free 4521K/4736K, paused 4ms+3ms, total 49ms
    09-02 08:22:40.513: D/gralloc_goldfish(2182): Emulator without GPU emulation detected.

please someone help me by correcting my code. thanks in advance.

John R
  • 2,058
  • 8
  • 33
  • 56
  • Unclear as to what you are actually asking, you seem to be changing the scores properly, however at a guess your problem is a null pointer because you never initialize the value of score. – Cob50nm Sep 02 '13 at 12:13
  • can u help me correcting code? – John R Sep 02 '13 at 12:28
  • replace `private int score` with `private static int score = 0;` – Cob50nm Sep 02 '13 at 12:44
  • when i answer the first question app stop working and showing error in log cat WAIT_FOR_CONCURRENT_GC block 20 or something ms. – John R Sep 02 '13 at 12:51
  • thats just the device clearing memory, gc stands for garbage collection, the error will be tagged E/AndroidRuntime or seomthing similar. and if you are using eclipse will be red – Cob50nm Sep 02 '13 at 13:09
  • i also know the meaning of gc. i posted code there can u check there's any error in questionactivity.class or gameplay.java class and how to solve gc error? plz help me. – John R Sep 02 '13 at 13:15
  • gc is not an error, the system will do this even if nothing is running on the device, it is just a memory management process. Without the rest of the logcat it is near impossible to determine where the error in the code is or what type of error it is – Cob50nm Sep 02 '13 at 13:28
  • so what can i do now? – John R Sep 02 '13 at 13:30
  • post the logcat, otherwise nothing – Cob50nm Sep 02 '13 at 13:36
  • logcat mainly showing errors in checkAnswer, getSelectedAnswer, findViewById, onClick, FATAL EXCEPTION. – John R Sep 02 '13 at 13:37
  • I need the actual print not just an interpretation, as this will give me specifically what the error is and the cause of it – Cob50nm Sep 02 '13 at 13:42
  • i updated file. you can see log cat file now. plz help me. – John R Sep 02 '13 at 13:56
  • you appear to have not given the emulator enough memory to deal with your application. Or you are calling something very recursive. `09-02 08:17:39.092: E/AndroidRuntime(2148): java.lang.StackOverflowError` I would suggest making your buttons and textviews `public static` so you only have to reference them once and updating the class activity you use initially instead of making a new one every time. As too many views seems to be the main cause of this kind of error – Cob50nm Sep 02 '13 at 14:06
  • i am unable to make textview and buttons static. can u explain me with my code? – John R Sep 03 '13 at 09:57
  • you need to declare them outside of your on create where you define your current game – Cob50nm Sep 03 '13 at 10:04
  • still not working. when i start emulator its show first question of quiz and when i answer that my app stop working. after that its restart. – John R Sep 03 '13 at 10:16
  • bro. you know or not? – John R Sep 03 '13 at 11:59
  • again you have just stated a very generic problem with no details about the type of problem. So no I have no idea – Cob50nm Sep 03 '13 at 12:01
  • in onClick method if i dont call !checkAnswer() method. my app work in every round but as u !checkAnswer method checking answer so its not check answer in this case. on the other hand if i call !checkAnswer as you able to see in my code. all errors come in log cat. i already sent you log cat file. plz help me. – John R Sep 03 '13 at 12:03
  • if you're still getting the same errors then you have not fixed the original problem. see this thread for ways to cut back on the nested views that are causing the error http://stackoverflow.com/questions/2762924/java-lang-stackoverflow-error-suspected-too-many-views – Cob50nm Sep 03 '13 at 12:08
  • i m not clear with that. i have already make buttons and textview static. – John R Sep 03 '13 at 12:19
  • I have told you the source of your problem and given you tools to fix it. Beyond that there is nothing I can do shy of fixing it myself which would require having the entire project and more time than I would be willing to spend on it. – Cob50nm Sep 03 '13 at 12:25
  • plz last time check the call to checkAnswer() method and its implementation. – John R Sep 03 '13 at 12:29

0 Answers0