Cannot invoke setValue on a background thread

question


LiveData更新数据报错解决方法java.lang.IllegalStateException: Cannot invoke setValue on a background thread

solve


将setValue(T)改为postValue(T)即可。

reason

MutableLiveData provides two functions postValue and setValue

  • setValue(T) must be called in the main thread,
  • postValue(T) can be called in the main thread or in the child thread;

public class MutableLiveData<T> extends LiveData<T> {
    @Override
    public void postValue(T value) 
    @Override
    public void setValue(T value)
}

Guess you like

Origin blog.csdn.net/Billy_Zuo/article/details/130269893