首页 > 解决方案 > 为什么即使在正确设置返回值非 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;
}

}

标签: androidandroid-livedata

解决方案


由于您在存储库类的类级别使用 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();
    }
}

推荐阅读