我在 Android 上开发,我想用下面的代码HandlerThread
来启动。countdownTimer
private var bgHandlerThread: HandlerThread? = HandlerThread("MyHandlerThread")
private fun startTimer() {
bgHandlerThread = HandlerThread("MyHandlerThread")
bgHandlerThread!!.start()
val bgHandler = Handler(bgHandlerThread!!.looper)
bgHandler.post {
countDownTimer = object : CountDownTimer(COUNT_DOWN_MAX_TIME.toLong(), COUNT_DOWN_INTERVAL.toLong()) {
override fun onTick(millisUntilFinished: Long) {
Log.d(TAG, "time:$millisUntilFinished ")
}
override fun onFinish() {
Log.d(TAG, "Timer countDown Finish ")
}
}.start()
}
}
但它显示以下错误
Attempt to read from field 'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
com.xx.Test.startTimer
所以我想startTimer()
将onLooperPrepared
.
在 Java 中,它类似于以下内容:
@Override
public void onLooperPrepared() {
}
但是我没有看到kotlin中的方法。onLooperPrepared
在 kotlin中使用很热门?
提前致谢。