ViewModel观察器方法返回null



我收到以下错误

Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe on a null object reference

从我的主片段中的以下代码部分

mReleasesViewModel = ViewModelProviders.of(this).get(ReleasesViewModel.class);
mReleasesViewModel.getUpcomingReleases(filter).observe(this, new Observer<List<_Release>>() {
@Override
public void onChanged(@Nullable List<_Release> releases) {
// whenever the list is changed
if (releases != null) {
mUpcomingGamesAdapter.setData(releases);
mUpcomingGamesAdapter.notifyDataSetChanged();
}
mDatabaseLoading.setVisibility(View.GONE);
}
}); 

错误特别出现在这条线上(当我连接观测者时(

mReleasesViewModel.getUpcomingReleases(filter) ...

我的ViewModel类:

public class ReleasesViewModel extends ViewModel {
// fragment name and list
private HashMap<String, MutableLiveData<List<_Release>>> upcomingReleasesListMap = new HashMap<>();
private ReleasesRepository releasesRepository;
private ArrayList<Integer> platforms;
private String region;
public ReleasesViewModel() {
// Shared to all fragments : User settings region & platforms
region = SharedPrefManager.read(SharedPrefManager.KEY_PREF_REGION, "North America");
Set<String> defaultPlatformsSet = new HashSet<>();
platforms = SharedPrefManager.read(SharedPrefManager.PLATFORM_IDS, defaultPlatformsSet);
}
public MutableLiveData<List<_Release>> getUpcomingReleases(String filter) {
// ReleasesRepository takes a different monthly filter
releasesRepository = new ReleasesRepository(region, filter, platforms);
if (upcomingReleasesListMap.containsKey(filter)) {
// Double check if it isn't null, just in case
if (upcomingReleasesListMap.get(filter) == null) {
// if null; try again to send a new request
loadReleases(filter);
} // else just don't do anything, the list is already in the Map
} else {
// Load it in if this filter was never added to the map [New filter and new list]
loadReleases(filter);
}
return upcomingReleasesListMap.get(filter);
}

private void loadReleases(final String filter) {
releasesRepository.addListener(new FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback<_Release>() {
@Override
public void onSuccess(List<_Release> result) {
// sort by release date
if (platforms.size() > 1) {
// Will only sort for multiple platforms filter
Collections.sort(result);
}
MutableLiveData<List<_Release>> releases = new MutableLiveData<>();
releases.setValue(result);
upcomingReleasesListMap.put(filter, releases);
}
@Override
public void onError(Exception e) {
// Log.e(TAG, e.getMessage());
MutableLiveData<List<_Release>> releases = new MutableLiveData<>();
releases.setValue(null);
upcomingReleasesListMap.put(filter, releases);
}
});
}
}

Hashmap是我将如何向片段提供数据的一个组成部分。由于我现在在应用程序中维护6个片段的方式,我需要为所有6个片段使用一个ViewModel。每个片段都有一个具有相同类型数据(对象和视图(的回收视图,但它们对该数据应用不同的过滤器。请查看ViewModel中的String filter参数,此参数将筛选器应用于数据。当然,我在ViewModel中维护所有6个片段。因为它们都需要同时更新(当用户完成诸如更改platforms列表之类的操作时(

您有异步代码,在实现这些方法时没有考虑这些代码,最终会得到一个空的LiveData,您调用observe((会抛出异常。您当前使用的流程:

  • 使用未知筛选器调用getUpcomingReleases((
  • 该筛选器不在映射中,因此您调用loadReleases((
  • loadReleases((只是为后台操作(firebase(设置一个监听器,并立即返回
  • 你会得到一行return upcomingReleasesListMap.get(filter(,它将返回null,因为firebase监听器很可能没有完成,并且您不能在该筛选器值的映射中放入任何内容
  • 使用null LiveData并失败

您需要类似以下代码的东西才能使其工作:

public MutableLiveData<List<_Release>> getUpcomingReleases(String filter) {
...
if (upcomingReleasesListMap.get(filter) == null) {
// we don't have a mapping for this filter so create one in the map
MutableLiveData<List<_Release>> releases = new MutableLiveData<>();
upcomingReleasesListMap.put(filter, releases);  
// also call this method to update the LiveData
loadReleases(filter);
}
// for now just return the empty LiveData so our ui can use it
// when the firebase listener returns we will update it
return upcomingReleasesListMap.get(filter);
}
private void loadReleases(final String filter) {
releasesRepository.addListener(new FirebaseDatabaseRepository.FirebaseDatabaseRepositoryCallback<_Release>() {
@Override
public void onSuccess(List<_Release> result) {
// sort by release date
if (platforms.size() > 1) {
// Will only sort for multiple platforms filter
Collections.sort(result);
}
// just use the previous created LiveData, this time with the data we got 
MutableLiveData<List<_Release>> releases = upcomingReleasesListMap.get(filter);
releases.setValue(result);
}
@Override
public void onError(Exception e) {
// Log.e(TAG, e.getMessage());
MutableLiveData<List<_Release>> releases = upcomingReleasesListMap.get(filter);
releases.setValue(null);                
}
});
}

相关内容

  • 没有找到相关文章

最新更新