我正在学习带数据绑定的MVVM,并且在TaskViewModel中存在空对象的问题。我创建了两个构造函数,其中一个为空,但是Task对象中为null。当我在viewmodel中只有一个构造函数时,viewmodel实例出错。我做错了什么? 如果您需要更多代码,请告诉我。
TaskViewModel
public class TaskViewModel extends ViewModel {
private TaskRepository mTaskRepository;
private LiveData<List<Task>> taskMutableLiveData;
private String TAG = "TaskViewModel = ";
private Context context;
public final ObservableField<String> description = new ObservableField<>();
public final ObservableField<String> date = new ObservableField<>();
public final ObservableField<String> time = new ObservableField<>();
public TaskViewModel(TaskRepository taskRepository) {
mTaskRepository = taskRepository;
}
public TaskViewModel() {
}
public void insert() {
addTask(description.get(), date.get(), time.get());
Log.d(TAG, "DATA " + description + " / " + date + " / " + time);
}
public void addTask(String description, String date, String time) {
Task addTask = new Task(description, date, time);
mTaskRepository.insert(addTask);
}
}
TaskRepository
public class TaskRepository {
private TaskDao taskDao;
private LiveData<List<Task>> allTasks;
public TaskRepository(Application application) {
TaskDatabase database = TaskDatabase.getInstance(application);
taskDao = database.taskDao();
allTasks = taskDao.getAllTasks();
}
public void insert(Task task) {
new InsertTaskAsyncTask(taskDao).execute(task);
}
public void update(Task task) {
new UpdateTaskAsyncTask(taskDao).execute(task);
}
public void delete(Task task) {
new DeleteTaskAsyncTask(taskDao).execute(task);
}
public LiveData<List<Task>> getAllTasks() {
return allTasks;
}
private static class InsertTaskAsyncTask extends AsyncTask<Task, Void, Void>
{
private TaskDao taskDao;
private InsertTaskAsyncTask(TaskDao taskDao) {
this.taskDao = taskDao;
}
@Override
protected Void doInBackground(Task... tasks) {
taskDao.insert(tasks[0]);
return null;
}
}
private static class DeleteTaskAsyncTask extends AsyncTask<Task, Void, Void>
{
private TaskDao taskDao;
private DeleteTaskAsyncTask(TaskDao taskDao) {
this.taskDao = taskDao;
}
@Override
protected Void doInBackground(Task... tasks) {
taskDao.delete(tasks[0]);
return null;
}
}
private static class UpdateTaskAsyncTask extends AsyncTask<Task, Void, Void>
{
private TaskDao taskDao;
private UpdateTaskAsyncTask(TaskDao taskDao) {
this.taskDao = taskDao;
}
@Override
protected Void doInBackground(Task... tasks) {
taskDao.update(tasks[0]);
return null;
}
}
}
答案 0 :(得分:1)
您无需在ViewModel
中使用参数化构造函数,而是在默认构造函数中进行初始化。因为毕竟我们要消耗ViewModel
中的ViewModelProviders
,{em}会从中调用默认构造函数(如果尚未自定义)。
所以请像下面这样
public class TaskViewModel extends ViewModel {
private TaskRepository mTaskRepository;
...
public TaskViewModel() {
mTaskRepository = new TaskRepository(); // This will avoid your null pointer exception
}
public void insert() {
addTask(description.get(), date.get(), time.get());
Log.d(TAG, "DATA " + description + " / " + date + " / " + time);
}
public void addTask(String description, String date, String time) {
Task addTask = new Task(description, date, time);
mTaskRepository.insert(addTask);
}
}
如果您想在Context
中包含ViewModel
,请改用AndroidViewModel
,它将在默认构造函数实现中为您提供应用程序上下文。但不要将其存储在您的ViewModel
中。