CountDownTimer琐事游戏得分 - Android电子(JAVA) [英] CountDownTimer trivia game score - Android (java)

查看:186
本文介绍了CountDownTimer琐事游戏得分 - Android电子(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个琐事游戏,显示10个问题,他们每个人都有10秒计时器。我有2个问题,无法正常工作。

首先,如果计时器耗尽的一个问题,它会显示下一个问题,但计时器没有复位。该textviews停留在时间到了!和经过时间:10000,而不是重新启动计时器上显示的新问题

最后,检索结果页面上的正确得分未在TextView中显示。百分比TextView中正确显示,但比分的TextView显示android.widget.TextView@416473c或一些其他随机的内存位置。

该项目从来没有崩溃只是函数不正确。任何code结构或其他建议是非常AP preciated!这是我的第一个Android手机应用的尝试,我慢慢地strugglingly通过它。然而,享受它! :)

QuesteionView.java

 公共类QuestionView延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.questionviewmain);        ANSWER1 =(按钮)findViewById(R.id.answer1);
        ANSWER2 =(按钮)findViewById(R.id.answer2);
        ANSWER3 =(按钮)findViewById(R.id.answer3);
        answer4 =(按钮)findViewById(R.id.answer4);        问题=(的TextView)findViewById(R.id.question);
        查询= getIntent()getParcelableArrayListExtra(查询)。
        计时器=(的TextView)findViewById(R.id.timer);
        timeElapsedView =(的TextView)findViewById(R.id.timeElapsedView);
        cdTimer =新的Timer(startTime时,间隔);
        loadQuestion();
    }    公共无效loadQuestion(){        如果(我== 9){
            endQuiz();
        }其他{
            如果(!timerHasStarted){
                cdTimer.start();
                timerHasStarted = TRUE;
            }其他{
                cdTimer.cancel();
                timerHasStarted = FALSE;
            }
            答案= queries.get(I).getCorrectAnswer();
            question.setText(queries.get(ⅰ).getQuery());            answer1.setText(queries.get(ⅰ).getA1());
            answer2.setText(queries.get(ⅰ).getA2());
            answer3.setText(queries.get(ⅰ).getA3());
            answer4.setText(queries.get(ⅰ).getA4());            answer1.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看为arg0){
                    queries.get(ⅰ).setSelectedAnswer(0);
                    如果(答案== 0){
                        correctAnswers ++;
                        下一个问题();
                    }其他{
                        wrongAnswers ++;
                        下一个问题();
                    }
                }
            });           //同类型code为答案2至4按钮。
                }
    }    公共无效nextQuestion(){
        得分=得分+ timeElapsed;
        我++;
        loadQuestion();
    }    公共类定时器扩展CountDownTimer {
        公共定时器(长的startTime,长间隔){
            超(startTime时,间隔);
        }        公共无效onFinish(){
            如果(我== 9){
                cdTimer.cancel();
            }其他{
                timer.setText(时间到!);
                timeElapsedView.setText(经过时间:+将String.valueOf(startTime时));
                wrongAnswers ++;
                下一个问题();
            }
        }        公共无效onTick(长millisUntilFinished){
            timer.setText(时间依然是:+ Long.toString(millisUntilFinished));
            timeElapsed =的startTime - millisUntilFinished;
            timeElapsedView.setText(经过时间:+ Long.toString(timeElapsed));
        }
    }    公共无效endQuiz(){
        意向意图=新意图(QuestionView.this,Results.class);
        intent.putExtra(correctAnswers​​,correctAnswers);
        intent.putExtra(wrongAnswers​​,wrongAnswers);
        intent.putExtra(得分,得分);
        intent.putParcelableArrayListExtra(查询,查询);
        startActivity(意向);
    }
}

Results.java

 公共类结果扩展活动{    QuestionView QV =新QuestionView();
    ArrayList的<问题> queryList = qv.getQueries();    INT cAnswers;
    INT wAnswers;    长分数;    ArrayList的<问题> QS;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.resultsmain);        cAnswers = getIntent()getIntExtra(correctAnswers​​,-1)。
        wAnswers = getIntent()getIntExtra(wrongAnswers​​,-1)。
        得分= getIntent()getLongExtra(得分,-1)。        QS = getIntent()getParcelableArrayListExtra(查询)。        按钮mainmenuBtn =(按钮)findViewById(R.id.mainmenuBtn);
        mainmenuBtn.setText(主菜单);        mainmenuBtn.setOnClickListener(新OnClickListener(){
            公共无效的onClick(查看为arg0){
                重新开始();
            }
        });        showResults();
    }    公共无效showResults(){        ArrayList的<&TextView的GT; TLIST =新的ArrayList<&TextView的GT;(9);        TextView的头=(的TextView)findViewById(R.id.header);
        header.setText(摘要);        TextView的百分比=(的TextView)findViewById(R.id.percentage);
        percentage.setText(Integer.toString(10 * cAnswers)+%);        TextView的分数=(的TextView)findViewById(R.id.score);
        字符串s =+得分;
        score.setText(多个);        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< tList.size();我++){
            tList.get(ⅰ).setText(qs.get(ⅰ).getQuery());
            如果(qs.get(ⅰ).getSelectedAnswer()== qs.get(ⅰ).getCorrectAnswer()){
                tList.get(ⅰ).setTextColor(Color.GREEN);
            }其他{
                tList.get(ⅰ).setTextColor(Color.RED);
            }
        }
    }    公共无效启动(){
        意向意图=新意图(Results.this,MainMenu.class);
        startActivity(意向);
    }
}


解决方案

从所有code,这是什么,我认为正在发生的事情。


  

首先,如果计时器耗尽的一个问题,它会显示下一个问题,但计时器没有复位。该textviews停留在时间到了!和经过时间:10000,而不是重新启动计时器上显示的新问题


这似乎是由于你没有时间之后你的 timerHasStarted 变量设置为false,用完了,所以我会设置为false可能当您加载你的下一个问题或后显示结果。


  

最后,检索结果页面上的正确得分未在TextView中显示。百分比TextView中正确显示,但比分的TextView显示android.widget.TextView@416473c或一些其他随机的内存位置。


这是因为你设置您的问与答变量TextView的和得到的ID。你需要像 q1.getText()。的toString()

您有相同名称的比分多个变量。因此,将其更改为

  TextView的score2 =(的TextView)findViewById(R.id.score);
  字符串s =+得分;
  score2.setText(多个);

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.

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.

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.

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! :)

QuesteionView.java

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);
    }
}

Results.java

    public class Results extends Activity {

    QuestionView qv = new QuestionView();
    ArrayList<Question> queryList = qv.getQueries();

    int cAnswers;
    int wAnswers;

    long score;

    ArrayList<Question> 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<TextView> tList = new ArrayList<TextView>(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 < 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);
    }
}

解决方案

From all of that code, this is what I think is happening

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.

This appears to be due to you not setting your timerHasStarted variable to false after the time runs out so I would set that to false probably when you load your next question or after you show the results.

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.

This is because you are setting your q variables to the textview and getting the id. You need something like q1.getText().toString()

You have multiple variables with the same name score. So change it to

  TextView score2 = (TextView)findViewById(R.id.score);
  String s = "" + score;
  score2.setText(s);

这篇关于CountDownTimer琐事游戏得分 - Android电子(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆