2

Can anyone see any reason why this would not be working?

 override fun resetAnimations() {
    Log.d("MainActivity", "start")
    Handler().postDelayed( { reset()}, 1500)
    Log.d("MainActivity", "end")
 }

 fun reset(){
     Log.d("MainActivity", "reset")
  }

I'm calling this in some arbitrary place in my activity but the reset() method is never called. In the logs I'm only getting the following

D/MainActivity: start

It looks like its blocking on postDelay.. even when I set the value to 1 or replace postDelay with pose, doesn't work.

Update:

when i implement like this, it works;

 private lateinit var handler : Handler

 override fun onCreate(savedInstanceState: Bundle?) {
    handler = Handler()
    resetAnimations()
 }

 override fun resetAnimations() {
    handler.postDelayed( { reset()}, 1500)
 }

I think the problem has something to do with the fact that I was calling resetAnimations() from a background thread, and creating the handler on the background thread..

4

1 回答 1

4

resetAnimations()我是从后台线程调用的。

那就是问题所在。您的处理程序无法将 a 发布Message到 a 上,因为您的后台线程上MessageQueue没有任何内容。MessageQueue

所以,而不是这个:

Handler().postDelayed( { reset()}, 1500)

执行此操作:

Handler(Looper.getMainThread()).postDelayed( { reset()}, 1500)
于 2017-09-28T10:45:20.993 回答