帮助我了解我在做什么错。片段中有一个项目列表。单击将打开一个具有完整描述的片段,并且有一个“添加到收藏夹”按钮。当我尝试致电更新model.update (data);
时引发错误
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void avocado.droid.ptitsami.room.DataDao.update(avocado.droid.ptitsami.room.Data)' on a null object reference
at avocado.droid.ptitsami.room.DataRepository$updateAsyncTask.doInBackground(DataRepository.java:54)
at avocado.droid.ptitsami.room.DataRepository$updateAsyncTask.doInBackground(DataRepository.java:44)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)`
但是模型不为空。在onChanged方法中,数据已成功设置。
DataDao
@Query("SELECT * FROM Data WHERE id = :id")
LiveData<Data> getById(int id);
@Update
void update(Data data);
DataRepository
LiveData<Data> getById(int id) {
return db.getDatabase().dataDao().getById(id);
}
void update(Data data) {
new updateAsyncTask(dataDao).execute(data);
}
private static class updateAsyncTask extends AsyncTask<Data, Void, Void> {
private DataDao mAsyncTaskDao;
updateAsyncTask(DataDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(final Data... params) {
mAsyncTaskDao.update(params[0]);
return null;
}
}
ViewModel
public class DataViewModel extends AndroidViewModel {
LiveData<Data> currentData;
DataRepository repository;
public DataViewModel(@NonNull Application application, final int dataId) {
super(application);
int verseId = dataId;
repository = new DataRepository(application);
currentData = repository.getById(verseId);
}
public LiveData<Data> getById() {
return currentData;
}
public void update(Data data) {
repository.update(data);
}
public static class ModelFactory extends ViewModelProvider.NewInstanceFactory {
@NonNull
private final Application application;
private final int dataId;
private final DataRepository repository;
public ModelFactory(@NonNull Application application, int id) {
super();
this.application = application;
this.dataId = id;
repository = new DataRepository(application);
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass == DataViewModel.class) {
return (T) new DataViewModel(application, dataId);
}
return null;
}
}
}
以及发生一切的片段方法
final DataViewModel.ModelFactory factory = new DataViewModel.ModelFactory(
getActivity().getApplication(), getArguments().getInt(KEY_DATA_ID));
final DataViewModel model = ViewModelProviders.of(this, factory)
.get(DataViewModel.class);
model.getById().observe(this, new Observer<Data>() {
@Override
public void onChanged(final Data data) {
tvTitle.setText(data.getTitle());
favorite = data.getFavorite();
if (favorite == 0) {
ivLike.setImageResource(R.drawable.heart_outline);
} else {
ivLike.setImageResource(R.drawable.heart_ok);
}
ivLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (favorite == 0) {
favorite = 1;
model.update(data);
ivLike.setImageResource(R.drawable.heart_ok);
anim = AnimationUtils.loadAnimation(getActivity(), R.anim.my_anim);
ivLike.startAnimation(anim);
} else {
}