android - 为什么即使在正确设置返回值非 null 之后也要改造返回 Null 'LiveData'?
问题描述
我得到了正确的响应,正确设置了 LiveData 的值(在将值设置为实时数据后通过在控制台上打印值得到确认)。但是当我试图在“返回”之前打印同样的东西时,它给了我 NullPointerException。
public class ProjectRepository {
private ProjectRepository instance;
Context context;
public ProjectRepository(Context context) {
this.context=context;
}
private MutableLiveData<List<PojoDivision>> data = new MutableLiveData<>();
public LiveData<List<PojoDivision>> getDivisionList() {
((RetrofitConfiguration)context).getDivisionRestApiWithAuthentication().getDivisionList().enqueue(new Callback<List<PojoDivision>>() {
@Override
public void onResponse(Call<List<PojoDivision>> call, Response<List<PojoDivision>> response) {
if (response.isSuccessful()) {
System.out.println(response.body().get(4).getName()); // this is printing
data.setValue(response.body());
System.out.println(data.getValue().get(4).getName()); // this is printing
}
}
@Override
public void onFailure(Call<List<PojoDivision>> call, Throwable t) {
Log.d(TAG, t.getMessage());
}
});
/*
following code is not printing with nullPointerException
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
*/
System.out.println(data.getValue().get(4).getName());
return data;
}
}
解决方案
由于您在存储库类的类级别使用 LiveData 对象,因此您的方法无需返回任何内容。相反,它负责更新 LiveData 对象。
public class ProjectRepository {
private static final String TAG = "ProjectRepository";
private ProjectRepository instance;
Context context;
public ProjectRepository(Context context) {
this.context = context;
}
public MutableLiveData<List<PojoDivision>> data = new MutableLiveData<>();
public void getDivisionList() {
((RetrofitConfiguration) context).getDivisionRestApiWithAuthentication().getDivisionList().enqueue(new Callback<List<PojoDivision>>() {
@Override
public void onResponse(Call<List<PojoDivision>> call, Response<List<PojoDivision>> response) {
if (response.isSuccessful()) {
data.postValue(response.body());
}
}
@Override
public void onFailure(Call<List<PojoDivision>> call, Throwable t) {
Log.d(TAG, t.getMessage());
}
});
}
}
此类的客户端需要 1) 观察 LiveData 对象,以及 2) 调用该getDivisionList()
方法来触发更新:
class MyFragment extends Fragment {
private ProjectRepository repository;
private void setRepository() {
Context context = getContext();
if (context == null) return;
repository = new ProjectRepository(context);
}
public void observeData() {
repository.data.observe(this, new Observer<List<PojoDivision>>() {
@Override
public void onChanged(List<PojoDivision> pojoDivisions) {
// do something with updated data
}
});
}
public void triggerUpdate() {
repository.getDivisionList();
}
}
推荐阅读
- javascript - 将 html 注入现有页面而不受 css 影响
- python - 列表理解的扩展导致列表幂集的无限循环
- c# - 会员加入频道的 Discord Bot 活动
- django - 如何在模板的数学表达式中使用 django forloop.counter0?
- swift - Swift:tableview 单元格上的 UIPanGesture 不起作用
- c# - 在 SQL Query 中用双引号替换单引号
- mysql - MYSQL 根据搜索过滤器将当前记录日期设置为上一个记录日期
- prolog - 简单的 Prolog Hangman 游戏错误
- dicom - Dicom 协议和 CT 扫描和根据轴向切片的 z 位置排序?
- matlab - 用 Matlab 解释神经人工网络训练中的梯度和性能结果