gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:26:41 24 4
gpt4 key购买 nike

我使用 Android 架构组件并尝试实例化 viewmodel 并从 LiveData 观察数据。但是我收到以下错误:

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

关于android - 尝试在空对象引用上调用虚拟方法 'void android.arch.lifecycle.MutableLiveData.setValue(java.lang.Object)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com