Answer the question
In order to leave comments, you need to log in
How to pass this to handler.postDelayed kotlin?
The error occurs in the this parameter passed to the handler.postDelayed method
class MainActivity : AppCompatActivity() {
private var s:Int = 0
private var running: Boolean = false
fun onClickStart(view: View) {
running = true
}
fun onClickStop(view: View) {
running = false
}
fun onClickReset(view: View) {
running = false
s = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runTimer()
}
private fun runTimer() {
val time_view = findViewById<TextView>(R.id.time_view)
val handler: Handler = Handler()
handler.post(
Runnable {
@Override
fun run() {
var hours:Int = s/3600
var minutes: Int = (s%3600)/60
var secs:Int = s%60
var time:String = String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, secs)
time_view.setText(time)
if(running) {
s++
}
handler.postDelayed(this,1000)
}
}
)
}
}
Answer the question
In order to leave comments, you need to log in
you have an error in the definition of Runnable
Runnable {
@Override
fun run() {
object: Runnable {
override fun run() {
handler.postDelayed(this)
}
}
Runnable {
//тут код, который должен быть в run()
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question