我正在使用paging library从房间获取recyclerview物品,尝试尝试新的新LivePagedListBuilder时出现nullPointerException,这是我的代码:
Activity.class
requestViewModel.request.getListenTo().observe(this,listenTo->{
if(listenTo == RequestViewModel.LISTEN_TO_ALL_RANDOM){
requestViewModel.getMedicalEntities().observe(this,pagedListEntity->{
entityRecyclerAdapter.submitList(pagedListEntity);
});
}
});
/**
* Search Text Observing
*/
requestViewModel.request.getSearchText().observe(this,searchText->{
if(searchText.length()>0){
requestViewModel.request.setListenTo(RequestViewModel.LISTEN_TO_SEARCH);
entityRecyclerAdapter.submitList(requestViewModel.makeFilterBySearch(searchText).getValue());
}else {
requestViewModel.request.setListenTo(RequestViewModel.LISTEN_TO_ALL_RANDOM);
}
});
RequestViewModel.class
private LiveData<PagedList<MedicalEntity>> medicalEntities;
private LiveData<PagedList<MedicalEntity>> searchEntitiesDataSource;
public RequestViewModel(AppRepository appRepository) {
request = new Request();
request.setListenTo(LISTEN_TO_ALL_RANDOM);
medicalEntities = new LivePagedListBuilder<>(appRepository.getRandomEntities(),3)
.setInitialLoadKey(0)
.build();
}
public LiveData<PagedList<MedicalEntity>> makeFilterBySearch(String search){
searchEntitiesDataSource = new LivePagedListBuilder<>(appRepository.getEntitiesBySearch(search),3)
.build();
return searchEntitiesDataSource;
}
public LiveData<PagedList<MedicalEntity>> getMedicalEntities() {
return medicalEntities;
}
Repository.class
public DataSource.Factory<Integer, MedicalEntity> getRandomEntities() {
initializeEntitiesData();
return entityDao.getEntities();
}
public DataSource.Factory<Integer,MedicalEntity> getEntitiesBySearch(String searchText) {
return entityDao.getEntitiesBySearch("%"+searchText+"%");
}
我收到NullPointerException错误
12-16 19:10:01.969 26717-26717/com.banquemisr.www.bmmedical E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.banquemisr.www.bmmedical, PID: 26717
java.lang.NullPointerException
at com.banquemisr.www.bmmedical.ui.requests.RequestViewModel.makeFilterBySearch(RequestViewModel.java:36)
at com.banquemisr.www.bmmedical.ui.requests.RequestsActivity.lambda$onCreate$2(RequestsActivity.java:106)
你能帮我吗?