Java 为什么我的按钮被冻结,无法识别任何点击?

Java 为什么我的按钮被冻结,无法识别任何点击?,java,android,Java,Android,我在做一个选择题游戏。您有4个单选按钮和一个按钮来浏览它们。时间用完后,您将看到答案,然后单击按钮继续下一个问题。问题是,在倒计时达到零后,您无法再单击按钮继续。当你点击按钮时,我试图改变问题的文本,看看它是否仍在反应和识别点击,但什么都没有发生。例如,您可以在on create方法中看到这些方法,其中我将侦听器设置为我的按钮并开始倒计时,或者在充满对象的大块(UpdateCountDownText和StartCountdown)之后使用实际时间方法。我不知道问题是在时间方法本身,还是我只是用错

我在做一个选择题游戏。您有4个单选按钮和一个按钮来浏览它们。时间用完后,您将看到答案,然后单击按钮继续下一个问题。问题是,在倒计时达到零后,您无法再单击按钮继续。当你点击按钮时,我试图改变问题的文本,看看它是否仍在反应和识别点击,但什么都没有发生。例如,您可以在on create方法中看到这些方法,其中我将侦听器设置为我的按钮并开始倒计时,或者在充满对象的大块(UpdateCountDownText和StartCountdown)之后使用实际时间方法。我不知道问题是在时间方法本身,还是我只是用错了。对不起,太乱了

问题在哪里?我感谢你的帮助

这是我的密码:

public class QuizActivity extends AppCompatActivity {

ImageView mImageView;
TextView mTextView;
TextView mAnswer;
TextView score;
TextView countDown;
RadioGroup radioGroup;
RadioButton opt1;
RadioButton opt2;
RadioButton opt3;
RadioButton opt4;
Button check;

boolean answered = false;


public static final long COUNTDOWN_IN_MILLISECODS = 30000;
public CountDownTimer countDownTimer;
public long timeLeftInMilliseconds;

private ColorStateList textColorDefaultCd;
private ColorStateList textColorDefaultRb;

int scoreNumber = 0;
int questionNumber = 0;
int questionNumberTotal = 9;
int arrayNumber = -1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    mImageView = findViewById(R.id.imageView);
    mTextView = findViewById(R.id.tvQ);
    mAnswer = findViewById(R.id.tvA);
    score = findViewById(R.id.tvScore);
    countDown = findViewById(R.id.tvCd);
    opt1 = findViewById(R.id.rb1);
    opt2 = findViewById(R.id.rb2);
    opt3 = findViewById(R.id.rb3);
    opt4 = findViewById(R.id.rb4);
    check = findViewById(R.id.btnCheck);
    radioGroup = findViewById(R.id.rG);

    textColorDefaultRb = opt1.getTextColors();

    textColorDefaultCd = countDown.getTextColors();

    shuffleQuestions();
    ShowNewQuestion();
    StartCountDown();

    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            check.setText("Bestätigen");
            if (!answered) {
                if (opt1.isChecked() || opt2.isChecked() || opt3.isChecked() || opt4.isChecked()) {
                    CheckAnswer();
                    countDownTimer.cancel();
                    answered = true;
                } else {
                    Toast.makeText(QuizActivity.this, "Wählen Sie eine Antwort aus", Toast.LENGTH_LONG).show();
                }
            } else if (answered) {
                ShowNewQuestion();
                StartCountDown();
                answered = false;
            }
        }

    });
}


Objects q01 = new Objects(R.drawable.blauwahl, "Das Herz eines Blauwals ist ungefähr so groß wie...", "ein VW-Käfer", "Angela Merkels Privatjet", "ein VW-Käfer", "eine Vespa", "eine Wassermelone");
Objects q02 = new Objects(R.drawable.simbabwe, "Der südafrikanische Staat Simbabwe wurde 1980 unabhängig. Wie hieß das Land früher? ", "Rhodesien", "Botswana", "Rhodesien", "Südafrika", "Botsuana");
Objects q03 = new Objects(R.drawable.sonnenkoenig, " Der französische Sonnenkönig Ludwig XIV. herrschte lange Jahre über Frankreich. Wie viele? ", "72", "22", "52", "62", "72");
Objects q04 = new Objects(R.drawable.tempelberg, "Der Tempelberg in Jerusalem ist rund 144.000 Quadratmeter groß und beladen mit Mythen. Wer soll hier den ersten Mord der Menschheit begangen haben?", "Kain", "David", "Kain", "Herodes", "Joseph");
Objects q05 = new Objects(R.drawable.frauenkirche, "In welchem Jahr des Zweiten Weltkrieges wurde die Dresdener Frauenkirche zerstört? ", "1945", "1943", "1944", "1945", "1939");
Objects q06 = new Objects(R.drawable.kueste, "Welches dieser europäischen Länder hat die längste Küste?", "Griechenland", "Italien", "Griechenland", "Frankreich", "Türkei");
Objects q07 = new Objects(R.drawable.umriss, "Der Umriss welches inselreichen Landes ist hier zu sehen?", "Dänemark", "Dänemark", "Philippinen", "Kroatien", "Griechenland");
Objects q08 = new Objects(R.drawable.blitz, "Woran liegt es, dass man den Donner bei manchen Blitzen erst viel später hört als bei anderen?", "An der unterschiedlichen Entfernung der Blitze", "An der unterschiedlichen Entfernung der Blitze", "An der Stärke der elektrischen Entladungen", "An der Dichte des Regens", "An den heftigen Windböen");
Objects q09 = new Objects(R.drawable.biathlon, "Aus welchen beiden Disziplinen setzt sich ein Biathlon zuammen?", "Sportschießen und Skilanglauf", "Sportschießen und Skilanglauf", " Skispringen und Skilanglauf ", " Skilanglauf und Abfahrtslauf ", "Abfahrtslauf und Skispringen ");
Objects q10 = new Objects(R.drawable.licht, "In welcher physikalischen Einheit wird die Lichtstärke gemessen?", "Candela", "Joule", "Lumen", "Lux", "Candela");
Objects q11 = new Objects(R.drawable.sonne, "Welcher Planet unseres Sonnensystems ist am weitesten von der Sonne entfernt?", "Neptun", "Uranus", "Jupiter", "Neptun", "Saturn");

Objects[] objectsArray = new Objects[]{
        q01, q02, q03, q04, q05, q06, q07, q08, q09, q10, q11
};

public void shuffleQuestions() {

    Collections.shuffle(Arrays.asList(objectsArray));
}

public void ShowNewQuestion() {
    if (questionNumber < questionNumberTotal) {

        opt1.setTextColor(textColorDefaultRb);
        opt2.setTextColor(textColorDefaultRb);
        opt3.setTextColor(textColorDefaultRb);
        opt4.setTextColor(textColorDefaultRb);

        radioGroup.clearCheck();
        answered = false;

        arrayNumber++;

        mTextView.setText(objectsArray[arrayNumber].getmQuestion());
        mImageView.setImageResource(objectsArray[arrayNumber].getmImages());
        opt1.setText(objectsArray[arrayNumber].getmOption1());
        opt2.setText(objectsArray[arrayNumber].getmOption2());
        opt3.setText(objectsArray[arrayNumber].getmOption3());
        opt4.setText(objectsArray[arrayNumber].getmOption4());
        mAnswer.setText(objectsArray[arrayNumber].getmAnswer());

        timeLeftInMilliseconds = COUNTDOWN_IN_MILLISECODS;
    } else {
        FinishQuiz();
    }
}

void StartCountDown() {
    countDownTimer = new CountDownTimer(timeLeftInMilliseconds, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            timeLeftInMilliseconds = millisUntilFinished;
            UpdateCountdownText();


        }

        @Override
        public void onFinish() {
            timeLeftInMilliseconds = 0;
            UpdateCountdownText();
            CheckAnswer();
            StartCountDown();

        }
    }.start();
}

public void ShowSolution() {
    answered = false;
    countDownTimer.cancel();
    if (opt1.getText().equals(mAnswer.getText())) {
        mTextView.setText("Nummer 1 ist richtig.");
        opt1.setTextColor(Color.GREEN);
        opt2.setTextColor(Color.RED);
        opt3.setTextColor(Color.RED);
        opt4.setTextColor(Color.RED);
    } else if (opt2.getText().equals(mAnswer.getText())) {

        mTextView.setText("Nummer 2 ist richtig.");
        opt1.setTextColor(Color.RED);
        opt2.setTextColor(Color.GREEN);
        opt3.setTextColor(Color.RED);
        opt4.setTextColor(Color.RED);
    } else if (opt3.getText().equals(mAnswer.getText())) {

        mTextView.setText("Nummer 3 ist richtig.");
        opt1.setTextColor(Color.RED);
        opt2.setTextColor(Color.RED);
        opt3.setTextColor(Color.GREEN);
        opt4.setTextColor(Color.RED);
    } else if (opt4.getText().equals(mAnswer.getText())) {

        mTextView.setText("Nummer 4 ist richtig.");
        opt1.setTextColor(Color.RED);
        opt2.setTextColor(Color.RED);
        opt3.setTextColor(Color.RED);
        opt4.setTextColor(Color.GREEN);
    }

    if (questionNumber < questionNumberTotal) {
        check.setText("Weiter");
        StartCountDown();
    } else {
        check.setText("Ergebnis");
        FinishQuiz();
    }
}

public void CheckAnswer() {
    answered = true;

    countDownTimer.cancel();

    if (opt1.isChecked()) {
        if (opt1.getText().equals(mAnswer.getText())) {
            scoreNumber++;
            questionNumber++;
            UpdateScore();
            countDownTimer.cancel();

        }

    } else if (opt2.isChecked()) {
        if (opt2.getText().equals(mAnswer.getText())) {
            scoreNumber++;
            questionNumber++;
            UpdateScore();
            countDownTimer.cancel();

        }

    }
    if (opt3.isChecked()) {
        if (opt3.getText().equals(mAnswer.getText())) {
            scoreNumber++;
            questionNumber++;
            UpdateScore();
            countDownTimer.cancel();

        }

    }
    else if (opt4.isChecked()) {
        if (opt4.getText().equals(mAnswer.getText())) {
            scoreNumber++;
            questionNumber++;
            UpdateScore();
            countDownTimer.cancel();

        } else {
            questionNumber++;
            countDownTimer.cancel();
        }
    }

    ShowSolution();
}

void UpdateCountdownText() {
    int minutes = (int) (timeLeftInMilliseconds / 1000) / 60;
    int seconds = (int) (timeLeftInMilliseconds / 1000) % 60;

    String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

    countDown.setText(timeFormatted);

    if (timeLeftInMilliseconds < 10000) {
        countDown.setTextColor(Color.RED);
    } else {
        countDown.setTextColor(textColorDefaultCd);
    }

    if (countDown.getText().equals("00:00")) {
        CheckAnswer();
        answered = true;
        check.setClickable(true);

    }

    };

    public void FinishQuiz () {

        Intent i = new Intent(QuizActivity.this, ResultActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("finalScore", scoreNumber + 1);
        i.putExtras(bundle);
        QuizActivity.this.finish();
        startActivity(i);
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

}
公共类quizaActivity扩展了AppCompative活动{
图像视图;
文本视图mTextView;
TextView mAnswer;
文本视图分数;
文本视图倒计时;
放射组放射组;
无线电按钮opt1;
无线电按钮opt2;
无线按钮opt3;
无线电按钮opt4;
按钮检查;
布尔值=假;
公共静态最终长倒计时单位为毫秒=30000;
公共倒计时;
公共长时间限制;
私有ColorStateList文本ColorDefaultCD;
私有ColorStateList textColorDefaultRb;
整数分数=0;
整数=0;
int-questionNumberTotal=9;
int arrayNumber=-1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u测验);
mImageView=findviewbyd(R.id.imageView);
mTextView=findviewbyd(R.id.tvQ);
mAnswer=findViewById(R.id.tvA);
分数=findViewById(R.id.tvScore);
倒计时=findViewById(R.id.tvCd);
opt1=findViewById(R.id.rb1);
opt2=findViewById(R.id.rb2);
opt3=findViewById(R.id.rb3);
opt4=findViewById(R.id.rb4);
检查=findViewById(R.id.btnCheck);
放射组=findViewById(R.id.rG);
textColorDefaultRb=opt1.getTextColors();
textColorDefaultCd=countDown.getTextColors();
shuffleQuestions();
ShowNewQuestion();
开始计数();
check.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
勾选.setText(“Bestätigen”);
如果(!回答){
如果(opt1.isChecked()| | opt2.isChecked()| | opt3.isChecked()| | opt4.isChecked()){
检查答案();
倒计时。取消();
回答=正确;
}否则{
Toast.makeText(QuizActivity.this,“你在吃蚂蚁草吗”,Toast.LENGTH_LONG.show();
}
}否则,如果(已回答){
ShowNewQuestion();
开始计数();
回答=错误;
}
}
});
}
对象q01=新对象(R.drawable.blauwahl,“这是一个很好的例子…”,“ein VW-Käfer”,“Angela Merkels Privatjet”,“ein VW-Käfer”,“eine Vespa”,“eine Wassermelone”);
对象q02=新对象(R.drawable.simbabwe,“südafrikanische Statat simbabwe wurde 1980 unabhängig.Wie hießdas Land früher?”,“罗得西亚”、“博茨瓦纳”、“罗得西亚”、“苏达弗里卡”、“博茨瓦纳”);
对象q03=新对象(R.drawable.sonnenkoenig,“französische Sonnenkönig Ludwig XIV.herrschte lange Jahreüber Frankreich.Wie viele?”,“72”,“22”,“52”,“62”,“72”);
Objects q04=新对象(R.drawable.tempelberg,“耶路撒冷的tempelberg运行了144000平方米的großund beladen mit Mythen。在哈本之前的一个月里,我们将继续使用它?”,“Kain”,“David”,“Kain”,“Herodes”,“Joseph”);
Objects q05=新对象(R.drawable.frauenkirche,“在德累斯顿世界银行中,frauenkirche zerstört?”,“1945”,“1943”,“1944”,“1945”,“1939”);
对象q06=新对象(R.drawable.kueste,“Welches dieser Europeäischen Länder hat die Längste Küste?”,“Griechenland”,“Italien”,“Griechenland”,“Frankreich”,“Türkei”);
Objects q07=新对象(R.drawable.umriss,“umriss welches inselreichen Landes Is hier zu sehen?”,“Dänemark”,“Dänemark”,“Philippin”,“Kroatien”,“Griechenland”);
对象q08=新对象(R.drawable.blitz,“你是谁,你是谁?”、“你是谁?”、“你是谁”、“你是谁”、“你是谁”、“你是谁”、“你是谁?”;
对象q09=新对象(R.drawable.biathlon,“我们在冬季两项比赛中的表现如何?”,“运动技巧与技巧”、“运动技巧与技巧”、“滑雪技巧与技巧”、“滑雪技巧与技巧”、“技巧与技巧”、“技巧与技巧与技巧”、“技巧与技巧与技巧”);
对象q10=新对象(R.drawable.licht,“在welcher Physicalischen Einheit wird die Lichtstärke gemessen?”,“坎德拉”、“焦耳”、“流明”、“勒克斯”、“坎德拉”);
天体q11=新天体(R.drawable.sonne,“Welcher行星未被发现的天体系统是否为weitesten von der sonne entfernt?”,“海王星”、“天王星”、“木星”、“海王星”、“土星”);
Objects[]objectsArray=新对象[]{
q01、q02、q03、q04、q05、q06、q07、q08、q09、q10、q11
};
公营机构{
Collections.shuffle(Arrays.asList(objectsArray));
}
公共问题{
if(问题编号<问题编号总计){
opt1.setTextColor(textColorDefaultRb);
opt2.setTextColor(textColorDefaultRb);
opt3.setTextColor(textColorDefaultRb);
opt4.setTextColor(textColorDefaultRb);
radioGroup.clearCheck();
回答=错误;
arrayNumber++;
mTextView.setText(objectsArray[arrayNumber].getmQuestion