gpt4 book ai didi

android - 尝试在空对象引用上调用虚方法 'void android.arch.lifecycle.LiveData.observe

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:41:24 26 4
gpt4 key购买 nike

我收到以下错误

我正在使用 Android 架构组件并尝试实例化 View 模型并从 LiveData 观察数据。但我被困在这里请帮我解决这个问题:

Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer)' on a null object reference

我的 fragment 是这样的:在调用 getCompany() 方法时我得到了 NullPoint Exception Error

    public class CompanyFragment extends LifecycleFragment {
private CompanyViewModel companyViewModel;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
companyViewModel = ViewModelProviders.of(this).get(CompanyViewModel.class);
companyViewModel.initData();
companyViewModel.getCompany().observe(this, new Observer<CompanyEntity>() {
@Override
public void onChanged(@Nullable CompanyEntity companyEntity) {

}
});
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_company, container, false);
}
}

我的 ViewModel 类是这样的:

public class CompanyViewModel extends ViewModel {
private LiveData<CompanyEntity> companyEntityLiveData;
private CompanyRepository companyRepo;

public CompanyViewModel() {}

public CompanyViewModel(CompanyRepository companyRepo){
this.companyRepo = companyRepo;
}

public void initData(){
companyEntityLiveData = companyRepo.getCompany();
}

public LiveData<CompanyEntity> getCompany() {
return companyEntityLiveData;
}

}

错误在这一行:

   companyViewModel.getCompany().observe(this, new Observer<CompanyEntity>() {
@Override
public void onChanged(@Nullable CompanyEntity companyEntity) {

}
});

这是我的 CompanyRepository 类:

public class CompanyRepository {
private WebService webService;

public CompanyRepository(){

}

public LiveData<CompanyEntity> getCompany() {
final MutableLiveData<CompanyEntity> data = new MutableLiveData<>();

webService.getCompany().enqueue(new Callback<CompanyEntity>() {
@Override
public void onResponse(Call<CompanyEntity> call, Response<CompanyEntity> response) {
data.setValue(response.body());
}

@Override
public void onFailure(Call<CompanyEntity> call, Throwable t) {

}
});

return data;
}
}

最佳答案

您必须观察您的 LiveData 的同一个实例。在您的 CompanyRepository 类中,您每次访问公司时都会创建一个新的 LiveData 实例,这意味着您的 Activity 随后会观察一个旧实例。

getCompany 方法应该更改为始终返回相同的 LiveData 实例。此外,您应该将 setValue() 更改为 postValue(),因为您正在尝试更新后台线程上的值。

final MutableLiveData<CompanyEntity> data = new MutableLiveData<>();
public LiveData<CompanyEntity> getCompany() {

webService.getCompany().enqueue(new Callback<CompanyEntity>() {
@Override
public void onResponse(Call<CompanyEntity> call, Response<CompanyEntity> response) {
data.postValue(response.body());
}

@Override
public void onFailure(Call<CompanyEntity> call, Throwable t) {

}
});

return data;
}

关于android - 尝试在空对象引用上调用虚方法 'void android.arch.lifecycle.LiveData.observe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986686/

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