Android 由于do while循环而出现白色屏幕

Android 由于do while循环而出现白色屏幕,android,android-studio,kotlin,do-while,Android,Android Studio,Kotlin,Do While,我对编码相当陌生,我正在尝试使用kotlin为android Studio编写我的第一个应用程序 每当我尝试在模拟器上启动我的应用程序时(使用以下代码),它只会显示一个白色屏幕 var useranswer = findViewById<EditText>(R.id.editNum1) var submitbutton = findViewById<Button>(R.id.submitbutton) var questions = findVie

我对编码相当陌生,我正在尝试使用kotlin为android Studio编写我的第一个应用程序

每当我尝试在模拟器上启动我的应用程序时(使用以下代码),它只会显示一个白色屏幕

    var useranswer = findViewById<EditText>(R.id.editNum1)
    var submitbutton = findViewById<Button>(R.id.submitbutton)
    var questions = findViewById<TextView>(R.id.aufgaben)
    var highscore = findViewById<TextView>(R.id.punktestand)
    var timer = findViewById<TextView>(R.id.timerrechts)
    
    var punkte: Int = 0
    
    submitbutton.setOnClickListener {
        
        object : CountDownTimer(60000,1000){
            override fun onFinish() {
                timer.text = "Zeit Abgelaufen!"
            }
            override fun onTick(millisUntilFinished: Long) {
                timer.text = "Zeit: " + millisUntilFinished / 1000
            }
        }.start()


        if (useranswer.text.toString().length <= 0){
            submitbutton.setText("Submit")
            highscore.setText("Highscore: " +punkte)
            
            do {
                
                var rnds11 = (1..20).random()
                var rnds12 = (1..20).random()
                var sum1: Int
                sum1 = rnds11 + rnds12
                questions.text = "$rnds11 + $rnds12 = "


                submitbutton.setOnClickListener {
                    if(useranswer.text.toString().toInt() == sum1) {
                        punkte = punkte + 1
                        highscore.setText("Punktestand: " + punkte)


                    }
                }

            }while (useranswer.text.toString().toInt() == sum1)
        }

    }
}
var useranswer=findviewbyd(R.id.editNum1)
var submitbutton=findviewbyd(R.id.submitbutton)
var问题=findViewById(R.id.aufgaben)
var highscore=findViewById(R.id.punktestand)
var timer=findviewbyd(R.id.timerrechts)
var punkte:Int=0
submitbutton.setOnClickListener{
对象:倒计时(600001000){
重写函数onFinish(){
timer.text=“Zeit Abgelaufen!”
}
覆盖趣味点击(毫秒直到完成:长){
timer.text=“Zeit:+millisnsuntiltfinished/1000
}
}.start()

if(useranswer.text.toString().length我不知道您是否在寻找这样的内容:

如果我没有错,您就不能在循环中使用
.setOnClickListener
事件,因为它将中断。我只是将您的变量设置为“全局”,并在出现Ticker时使用您的实现,所以您尝试在pass为倒计时时执行一些操作。请告诉我是否正确,因为它看起来很有趣


Ps.在这个例子中。我在第一个字段中放了数字5来表示观点哈哈

我不知道你是否在寻找这样的东西:

如果我没有错,您就不能在循环中使用
.setOnClickListener
事件,因为它将中断。我只是将您的变量设置为“全局”,并在出现Ticker时使用您的实现,所以您尝试在pass为倒计时时执行一些操作。请告诉我是否正确,因为它看起来很有趣

Ps.在这个例子中。我在第一个字段中放了数字5来表示观点哈哈

试着这样做:

var useranswer = findViewById<EditText>(R.id.editNum1)
var submitbutton = findViewById<Button>(R.id.submitbutton)
var questions = findViewById<TextView>(R.id.aufgaben)
var highscore = findViewById<TextView>(R.id.punktestand)
var timer = findViewById<TextView>(R.id.timerrechts)
var rnds11:Int
var rnds12:Int
var sum1: Int
var countDown: CountDownTimer

var punkte: Int = 0

submitbutton.setOnClickListener {
    // stop the existing countDownTimer if it is not null. countDown.stop() or similar. then...
    if (useranswer.text.toString().toInt() == sum1) {
    punkte = punkte + 1
    highscore.setText("Punktestand: " + punkte)
    countDown : CountDownTimer(60000,1000){
        override fun onFinish() {
            timer.text = "Zeit Abgelaufen!"
        }
        override fun onTick(millisUntilFinished: Long) {
            timer.text = "Zeit: " + millisUntilFinished / 1000
        }
    }.start()

        submitbutton.setText("Submit")
        highscore.setText("Highscore: " +punkte)
            
            rnds11 = (1..20).random()
            rnds12 = (1..20).random()
            sum1 = rnds11 + rnds12
            questions.text = "$rnds11 + $rnds12 = "
    } else {
          //Failed. Do your work
    }

}
}
var useranswer=findviewbyd(R.id.editNum1)
var submitbutton=findviewbyd(R.id.submitbutton)
var问题=findViewById(R.id.aufgaben)
var highscore=findViewById(R.id.punktestand)
var timer=findviewbyd(R.id.timerrechts)
变量rnds11:Int
变量rnds12:Int
变量sum1:Int
var倒计时:倒计时
var punkte:Int=0
submitbutton.setOnClickListener{
//如果现有的countDownTimer不为null,请停止它。countDown.stop()或类似值。然后。。。
if(useranswer.text.toString().toInt()==sum1){
朋克特=朋克特+1
highscore.setText(“Punktestand:+punkte”)
倒计时:倒计时(600001000){
重写函数onFinish(){
timer.text=“Zeit Abgelaufen!”
}
覆盖趣味点击(毫秒直到完成:长){
timer.text=“Zeit:+millisnsuntiltfinished/1000
}
}.start()
submitbutton.setText(“提交”)
highscore.setText(“highscore:+punkte)
rnds11=(1..20).random()
rnds12=(1..20).random()
sum1=rnds11+rnds12
questions.text=“$rnds11+$rnds12=”
}否则{
//失败了,做你的工作
}
}
}
您正在侦听器中创建一个Neverening循环。侦听器不应在do-while循环中创建自己(除非出现预期情况),但他们执行代码然后离开。在我的示例中,当用户按下按钮时,首先检查答案是否正确。然后重置计时器,更新分数,检查是否正确,然后用新值再次启动计时器。

尝试这样做:

var useranswer = findViewById<EditText>(R.id.editNum1)
var submitbutton = findViewById<Button>(R.id.submitbutton)
var questions = findViewById<TextView>(R.id.aufgaben)
var highscore = findViewById<TextView>(R.id.punktestand)
var timer = findViewById<TextView>(R.id.timerrechts)
var rnds11:Int
var rnds12:Int
var sum1: Int
var countDown: CountDownTimer

var punkte: Int = 0

submitbutton.setOnClickListener {
    // stop the existing countDownTimer if it is not null. countDown.stop() or similar. then...
    if (useranswer.text.toString().toInt() == sum1) {
    punkte = punkte + 1
    highscore.setText("Punktestand: " + punkte)
    countDown : CountDownTimer(60000,1000){
        override fun onFinish() {
            timer.text = "Zeit Abgelaufen!"
        }
        override fun onTick(millisUntilFinished: Long) {
            timer.text = "Zeit: " + millisUntilFinished / 1000
        }
    }.start()

        submitbutton.setText("Submit")
        highscore.setText("Highscore: " +punkte)
            
            rnds11 = (1..20).random()
            rnds12 = (1..20).random()
            sum1 = rnds11 + rnds12
            questions.text = "$rnds11 + $rnds12 = "
    } else {
          //Failed. Do your work
    }

}
}
var useranswer=findviewbyd(R.id.editNum1)
var submitbutton=findviewbyd(R.id.submitbutton)
var问题=findViewById(R.id.aufgaben)
var highscore=findViewById(R.id.punktestand)
var timer=findviewbyd(R.id.timerrechts)
变量rnds11:Int
变量rnds12:Int
变量sum1:Int
var倒计时:倒计时
var punkte:Int=0
submitbutton.setOnClickListener{
//如果现有的countDownTimer不为null,请停止它。countDown.stop()或类似值。然后。。。
if(useranswer.text.toString().toInt()==sum1){
朋克特=朋克特+1
highscore.setText(“Punktestand:+punkte”)
倒计时:倒计时(600001000){
重写函数onFinish(){
timer.text=“Zeit Abgelaufen!”
}
覆盖趣味点击(毫秒直到完成:长){
timer.text=“Zeit:+millisnsuntiltfinished/1000
}
}.start()
submitbutton.setText(“提交”)
highscore.setText(“highscore:+punkte)
rnds11=(1..20).random()
rnds12=(1..20).random()
sum1=rnds11+rnds12
questions.text=“$rnds11+$rnds12=”
}否则{
//失败了,做你的工作
}
}
}

您正在侦听器中创建一个Neverening循环。侦听器不应在do-while循环中创建自己(除非出现预期情况),但他们执行代码然后离开。在我的示例中,当用户按下按钮时,您首先检查答案是否正确。然后重置计时器,更新分数,检查是否正确,然后用新值再次启动计时器。

您能编辑您的问题并添加UI吗?我将尝试复制您的问题nk问题是,等待单击按钮不是阻塞操作;在您的示例中发生的是do..while循环运行一次(除非它恰好以
useranswer
中已经存在的相同数字结束,然后再也不会出现。如果你将整个检查部分移动到
setOnClickListener
中并摆脱循环,每次单击按钮时都会发生。@AntonioLabra我将我的UI添加到我的问题中------Joozd听起来不错,我会尝试一下你说,谢谢。你能编辑你的问题吗?请添加你的UI?我会尝试复制你的问题。我认为问题在于等待点击按钮不是阻止动作;在你的例子中发生的是do..while循环运行一次(除非它恰好以相同的数字结束)