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

时间:2018-02-21 08:52:12

标签: android android-livedata

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

ActivationFragment.clas:

'at start of program
    Dim q As New Queue(Of Bitmap)

'when images are received
    q.Enqueue(img)

'part of the timer
    Dim tmpbmp As Bitmap = q.First.Clone
    img_sobel.Image = ReturnSobel(tmpbmp)
    tmpbmp = Nothing
    q.Dequeue()

ActivationViewModel类:

@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) {
        } 
    });
}

}

ActivationRepository类:

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);
}

1 个答案:

答案 0 :(得分:5)

像这样更新你的功能..

public class ActivationRepository {

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

 public MutableLiveData<Boolean> getStatusCode(String activationCode) {
    status.postValue(Boolean.valueOf(false));
    return status;
}