我收到以下错误
我正在使用Android架构组件,并试图从LiveData实例化viewmodel和Observe数据。但我坚持到这里请帮助我解决这个问题:
Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer)' on a null object reference
My Fragment是这样的: 在调用getCompany()方法时获取和NullPoint异常错误
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;
}
}
答案 0 :(得分:1)
您必须观察LiveData的相同实例。在CompanyRepository
课程中,每次访问公司时都会创建一个新的LiveData实例,这意味着您的活动正在观察旧实例。
应该将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;
}
答案 1 :(得分:1)
更改
public CompanyViewModel(){
companyRepo= new companyRepo();
}
public LiveData<CompanyEntity> getCompany() {
if(companyEntityLiveData==null)
companyEntityLiveData= companyRepo.getSection();
return companyEntityLiveData;
}
在您的片段中:
CompanyViewModel companyViewModel=
ViewModelProviders.of(this).get(CompanyViewModel .class);
LiveData<CompanyEntity> s = companyViewModel.getCompany();
s.observe(this, new Observer<CompanyEntity>() {
@Override
public void onChanged(@Nullable CompanyEntity companyEntity) {
Toast.makeText(getContext(),"Go go go", Toast.LENGTH_LONG).show();
}
});
答案 2 :(得分:0)
在getCompany方法中,您需要检查companyEntityLiveData对象是否为null,如果是,则调用service中的服务。
public LiveData<CompanyEntity> getCompany() {
if(companyEntityLiveData == null){
companyEntityLiveData = companyRepo.getCompany();
}
return companyEntityLiveData;
}
答案 3 :(得分:0)
问题是您正在尝试使用尚未初始化的内容。 因此,为了不依赖于执行顺序,我们必须在存储存储库返回内容的变量中建立安全的可操作性 private val nullableObj: LiveData<T2>?
有了这个,我们甚至可以在存储库加载属性之前就使用该属性。在以下示例中检查执行顺序:
val aUse: LiveData<T> = MediatorLiveData<T>().apply {
nullableObj?.let {
}
}
private val nullableObj: LiveData<T2>? = repository::getNullableObj
在使用存储库#getCompany()之前回答您的问题,检查可为空性。在科特林:
companyVM.company?.observe(...) { ... }
GL