尝试在空对象引用上调用虚方法'void android.arch.lifecycle.MutableLiveData.setValue(java.lang.Object)'

问题描述 投票:0回答:1

我使用Android架构组件并尝试从LiveData实例化viewmodel和Observe数据。但我得到以下错误:

ActivationFragment.clas:

@OnClick(R.id.btn_activation)
public void onEvaluateClick(Button v) {
    maActivationCode = etActivation.getText().toString();
    mActiationViewModel.getsatusCode(maActivationCode).observe(this, new Observer<Boolean>() {
        @Override
        public void onChanged(@Nullable Boolean aBoolean) {
        } 
    });
}

ActivationViewModel类:

public class ActivationViewModel  extends ViewModel{

public ActivationRepository activationRepository;

public ActivationViewModel(ActivationRepository activationRepository) {
    this.activationRepository = activationRepository;
}

public LiveData<Boolean> getsatusCode(String activationCode) {
    return (LiveData<Boolean>)  activationRepository.getStatusCode(activationCode);
}

}

ActivationRepository类:

public class ActivationRepository {

public MutableLiveData<Boolean> status;

public MutableLiveData<Boolean> getStatusCode(String activationCode) {
    status.setValue(Boolean.valueOf(false));
    return status;
}
android android-livedata
1个回答
5
投票

像这样更新你的功能..

public class ActivationRepository {

public MutableLiveData<Boolean> status = new MutableLiveData<>();

 public MutableLiveData<Boolean> getStatusCode(String activationCode) {
    status.postValue(Boolean.valueOf(false));
    return status;
}
© www.soinside.com 2019 - 2024. All rights reserved.