在postDelayed之后,视图不能为null

时间:2018-12-29 12:31:57

标签: android kotlin handler runnable

我尝试与Handler重复动画任务

   import kotlinx.android.synthetic.main.fragment_main.*
   ...
   ...
   ...
   private val mInterval: Long = 3000
   private var mHandler: Handler = Handler()
   private var mStatusChecker: Runnable = object : Runnable {
    override fun run() {
                txtUserRole.text = "dddddddddd"
                YoYo.with(Techniques.Pulse)
                    .duration(700)
                    .playOn(txtUserRole)
            mHandler.postDelayed(this, mInterval)
    }
}

private fun updateStatus() {

}

fun startRepeatingTask(txtUserRole: TextView) {
    mStatusChecker.run()
}

fun stopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker)
}

在第一次运行时,一切正常,但是在第一次重复后, txtUserRole NULL

1 个答案:

答案 0 :(得分:0)

最后,我尝试在没有处理程序的情况下重复动画

 YoYo.with(Techniques.Pulse)
            .duration(700)
            .delay(3000)
            .withListener(
                object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {}
                    override fun onAnimationEnd(animation: Animator) { animation.start() }
                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                })
            .playOn(txtUserRole)