android – ViewModel observer方法返回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 one rror(Exception e) {
// Log.e(TAG, e.getMessage());
MutableLiveData<List<_Release>> releases = new MutableLiveData<>();
releases.setValue(null);
upcomingReleasesListMap.put(filter, releases);
}
});
}
}
Hashmap是我如何向片段提供数据的不可或缺的一部分.由于我现在如何在我的应用程序中维护6个片段,我需要为所有六个片段使用一个ViewModel.每个片段都有一个具有相同类型数据(对象和视图)的recyclelerview,但它们对该数据应用不同的过滤器.请查看ViewModel中的String filter参数,该参数将过滤器应用于数据.当然,我在ViewModel中维护所有6个片段.因为它们都需要同时更新(当用户完成更改平台列表等操作时)
解决方法:
您有异步代码,在实现这些方法时没有考虑这些异步代码,并且您最终得到一个null的LiveData,您可以在其上调用observe()抛出异常.您目前使用的流程:
>使用未知过滤器调用getUpcomingReleases()
>此过滤器不在地图中,因此您调用loadReleases()
> loadReleases()只为后台操作(firebase)设置一个监听器并立即返回
>你得到的行返回comingReleasesListMap.get(过滤器);这将返回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 one rror(Exception e) {
// Log.e(TAG, e.getMessage());
MutableLiveData<List<_Release>> releases = upcomingReleasesListMap.get(filter);
releases.setValue(null);
}
});
}
标签:android,viewmodel,android-livedata 来源: https://codeday.me/bug/20190705/1387618.html