android - 我的 android 测验应用程序在 android studio 中不断崩溃
问题描述
我正在开发一个测验应用程序,我遇到了一个我的 Countdowntimer 一直在我分配给它的 textView 中显示随机值,并且选项按钮在单击后不会恢复为我分配给它的颜色这里是我的代码
public void selectEasy(View view){
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
view.setBackgroundResource(R.drawable.button_right);
score++;
} else{
view.setBackgroundResource(R.drawable.button_wrong);
if (button0.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
button0.setBackgroundResource(R.drawable.button_right);
}else if (button1.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
button1.setBackgroundResource(R.drawable.button_right);
}else if (button2.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
button2.setBackgroundResource(R.drawable.button_right);
}else if (button3.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
button3.setBackgroundResource(R.drawable.button_right);
}
}
GenerateQuestion();
}
生成随机问题的第二种方法
public void GenerateQuestion(){
question++;
Easy_score.setText(Integer.toString(score));
Easy_question.setText(Integer.toString(question) + "/"+ 10);
Easy_time.setText(10 + "s");
button0.setBackgroundResource(R.drawable.button_style);
button1.setBackgroundResource(R.drawable.button_style);
button2.setBackgroundResource(R.drawable.button_style);
button3.setBackgroundResource(R.drawable.button_style);
new CountDownTimer(10100, 1000){
@Override
public void onTick(long millinseconds) {
Easy_time.setText(String.valueOf(millinseconds / 1000) + "s");
}
@Override
public void onFinish() {
GenerateQuestion();
}
}.start();
int[] picture = {};
String[] incorrectOption = {};
Random random = new Random();
displayPicture = random.nextInt(21);
display.setImageResource(picture[displayPicture]);
answers.clear();
locationOfCorrectAnswer = random.nextInt(4);
for (int i = 0; i < 4; i++){
if (i == locationOfCorrectAnswer){
answers.add(getResources().getResourceEntryName(picture[displayPicture]));
} else {
LocationOfWrongAnswer = random.nextInt(36);
while (LocationOfWrongAnswer == displayPicture) {
LocationOfWrongAnswer = random.nextInt(36);
}
answers.add(incorrectOption[LocationOfWrongAnswer]);
}
}
button0.setText(" A. " + answers.get(0).substring(4).replace("_"," ").replace("1", "-"));
button1.setText(" B. " + answers.get(1).substring(4).replace("_"," ").replace("1", "-"));
button2.setText(" C. " + answers.get(2).substring(4).replace("_"," ").replace("1", "-"));
button3.setText(" D. " + answers.get(3).substring(4).replace("_"," ").replace("1", "-"));
}
当单击按钮并且按钮未恢复为初始颜色时,Countdowntimer 显示随机值
解决方案
推荐阅读
- module - 如何在 Drupal 中单击一下就克隆一个节点及其所有子节点?
- html - 从文件中检查多个 url
- javascript - 谷歌云端硬盘文件选择器被谷歌阻止
- javascript - Javascript SetMonth 问题
- next.js - 滚动到 nextjs 中的部分
- azure - 如何在不创建应用程序的情况下使用图形 API 上传到 onedrive
- php - 用 php 下载 svg
- tensorflow - 为什么在 TensorFlow 模型训练中 keras 比 estimator 慢?
- python - 是否可以使用我的帐户而不是机器人 DM 用户?不和谐
- python - 词义消歧 WordNet