Android rxKotlin在Subscribe合并中崩溃

时间:2018-12-09 15:09:24

标签: android kotlin rx-kotlin2

当代码和名称不为空时,我需要enable/disable按钮。

我的代码:

btnAddItem.isEnabled = false

    val codeIsValid = RxTextView.textChanges(txvCode)
        .debounce(350, TimeUnit.MILLISECONDS)
        .map { code ->
            code.isNotEmpty()
        }

    val nameIsValid = RxTextView.textChanges(edtName)
        .debounce(350, TimeUnit.MILLISECONDS)
        .map { name ->
            name.isNotEmpty()
        }

    disposableEnableButtonSave = Observables.combineLatest(codeIsValid, nameIsValid) 
        { b1, b2 -> b1 && b2 }
        .subscribe {
            if (btnAddItem.isEnabled != it){
                btnAddItem.isEnabled = it //crash here.
            }
        }

但是运行时出错:

Logcat:

  

io.reactivex.exceptions.OnErrorNotImplementedException:动画师只能在Looper线程上运行
  android.util.AndroidRuntimeException:动画师只能在Looper个线程上运行

代码崩溃是enable/disable按钮。

btnAddItem.isEnabled = it

1 个答案:

答案 0 :(得分:0)

尝试

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (btnAddItem.isEnabled != it){
               btnAddItem.isEnabled = it
            }
        }
});