Android rxKotlin 在订阅 combineLatest 时崩溃
Android rxKotlin crash in subcribe combineLatest
当代码和名称不为空时,我想要 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: Animators may only be run on Looper threads
android.util.AndroidRuntimeException: Animators may only be run on Looper
threads
代码崩溃是 enable/disable
按钮。
btnAddItem.isEnabled = it
试试这个
runOnUiThread(new Runnable() {
@Override
public void run() {
if (btnAddItem.isEnabled != it){
btnAddItem.isEnabled = it
}
}
});
当代码和名称不为空时,我想要 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: Animators may only be run on Looper threads
android.util.AndroidRuntimeException: Animators may only be run onLooper
threads
代码崩溃是 enable/disable
按钮。
btnAddItem.isEnabled = it
试试这个
runOnUiThread(new Runnable() {
@Override
public void run() {
if (btnAddItem.isEnabled != it){
btnAddItem.isEnabled = it
}
}
});