Note that there are some explanatory texts on larger screens.

plurals
  1. POCountDownTimer trivia game score - Android (java)
    primarykey
    data
    text
    <p>I have a trivia game that displays 10 questions and they each have a 10 second timer. I have 2 problems that do not function correctly. </p> <p>Firstly, If the timer runs out on a question, it displays the next question but the timer does not reset. The textviews stay at "Time's up!" and "Time Elapsed: 10000" instead of restarting the timer on the new question that is displayed.</p> <p>Lastly, on the Results page the correct score is not displayed in the textview. The percentage textview displays correctly but the score textview displays "android.widget.TextView@416473c" or some other random memory location.</p> <p>The program never crashes just functions incorrectly. Any code structure or other suggestions is much appreciated! This is my first android mobile app attempt and I am slowly and strugglingly through it. Yet enjoying it! :)</p> <p>QuesteionView.java</p> <pre><code>public class QuestionView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.questionviewmain); answer1 = (Button)findViewById(R.id.answer1); answer2 = (Button)findViewById(R.id.answer2); answer3 = (Button)findViewById(R.id.answer3); answer4 = (Button)findViewById(R.id.answer4); question = (TextView)findViewById(R.id.question); queries = getIntent().getParcelableArrayListExtra("queries"); timer = (TextView)findViewById(R.id.timer); timeElapsedView = (TextView)findViewById(R.id.timeElapsedView); cdTimer = new Timer(startTime, interval); loadQuestion(); } public void loadQuestion() { if(i == 9) { endQuiz(); } else { if(!timerHasStarted) { cdTimer.start(); timerHasStarted = true; } else { cdTimer.cancel(); timerHasStarted = false; } answer = queries.get(i).getCorrectAnswer(); question.setText(queries.get(i).getQuery()); answer1.setText(queries.get(i).getA1()); answer2.setText(queries.get(i).getA2()); answer3.setText(queries.get(i).getA3()); answer4.setText(queries.get(i).getA4()); answer1.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { queries.get(i).setSelectedAnswer(0); if(answer == 0) { correctAnswers++; nextQuestion(); } else { wrongAnswers++; nextQuestion(); } } }); //same type of code for buttons for answers 2 through 4. } } public void nextQuestion() { score = score + timeElapsed; i++; loadQuestion(); } public class Timer extends CountDownTimer { public Timer(long startTime, long interval) { super(startTime, interval); } public void onFinish() { if(i == 9) { cdTimer.cancel(); } else { timer.setText("Time's up!"); timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime)); wrongAnswers++; nextQuestion(); } } public void onTick(long millisUntilFinished) { timer.setText("Time remain: " + Long.toString(millisUntilFinished)); timeElapsed = startTime - millisUntilFinished; timeElapsedView.setText("Time Elapsed: " + Long.toString(timeElapsed)); } } public void endQuiz() { Intent intent = new Intent(QuestionView.this, Results.class); intent.putExtra("correctAnswers", correctAnswers); intent.putExtra("wrongAnswers", wrongAnswers); intent.putExtra("score", score); intent.putParcelableArrayListExtra("queries", queries); startActivity(intent); } } </code></pre> <p>Results.java</p> <pre><code> public class Results extends Activity { QuestionView qv = new QuestionView(); ArrayList&lt;Question&gt; queryList = qv.getQueries(); int cAnswers; int wAnswers; long score; ArrayList&lt;Question&gt; qs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.resultsmain); cAnswers = getIntent().getIntExtra("correctAnswers", -1); wAnswers = getIntent().getIntExtra("wrongAnswers", -1); score = getIntent().getLongExtra("score", -1); qs = getIntent().getParcelableArrayListExtra("queries"); Button mainmenuBtn = (Button)findViewById(R.id.mainmenuBtn); mainmenuBtn.setText("Main Menu"); mainmenuBtn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { restart(); } }); showResults(); } public void showResults() { ArrayList&lt;TextView&gt; tList = new ArrayList&lt;TextView&gt;(9); TextView header = (TextView)findViewById(R.id.header); header.setText("SUMMARY"); TextView percentage = (TextView)findViewById(R.id.percentage); percentage.setText(Integer.toString(10 * cAnswers) + "%"); TextView score = (TextView)findViewById(R.id.score); String s = "" + score; score.setText(s); TextView q1 = (TextView)findViewById(R.id.q1); TextView q2 = (TextView)findViewById(R.id.q2); TextView q3 = (TextView)findViewById(R.id.q3); TextView q4 = (TextView)findViewById(R.id.q4); TextView q5 = (TextView)findViewById(R.id.q5); TextView q6 = (TextView)findViewById(R.id.q6); TextView q7 = (TextView)findViewById(R.id.q7); TextView q8 = (TextView)findViewById(R.id.q8); TextView q9 = (TextView)findViewById(R.id.q9); TextView q10 = (TextView)findViewById(R.id.q10); tList.add(q1); tList.add(q2); tList.add(q3); tList.add(q4); tList.add(q5); tList.add(q6); tList.add(q7); tList.add(q8); tList.add(q9); tList.add(q10); for(int i = 0; i &lt; tList.size(); i++) { tList.get(i).setText(qs.get(i).getQuery()); if(qs.get(i).getSelectedAnswer() == qs.get(i).getCorrectAnswer()) { tList.get(i).setTextColor(Color.GREEN); } else { tList.get(i).setTextColor(Color.RED); } } } public void restart() { Intent intent = new Intent(Results.this, MainMenu.class); startActivity(intent); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload