我使用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;
}
像这样更新你的功能..
public class ActivationRepository {
public MutableLiveData<Boolean> status = new MutableLiveData<>();
public MutableLiveData<Boolean> getStatusCode(String activationCode) {
status.postValue(Boolean.valueOf(false));
return status;
}