Ошибка Cannot create an instance of class space.rodionov.architecturedriller.NoteViewModel. Ищу решение
Приложение выдает ошибку:
Caused by: java.lang.RuntimeException: Cannot create an instance of class space.rodionov.architecturedriller.NoteViewModel
с указанием на строку:
noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
из MainActivity
Так же высвечивается ошибка
java.lang.Class<space.rodionov.architecturedriller.NoteViewModel> has no zero argument constructor"
В чем может быть ошибка?
Код из MainActivity:
public class MainActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
//update RecyclerView
Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_SHORT).show();
}
});
}
}
Код из NoteViewModel:
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Note>> allNotes;
public NoteViewModel(@NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = repository.getAllNotes();
}
public void insert(Note note) {
repository.insert(note);
}
public void update(Note note) {
repository.insert(note);
}
public void delete(Note note) {
repository.insert(note);
}
public void deleteAllNotes() {
repository.deleteAllNotes();
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
Код из NoteRepository:
public class NoteRepository {
private NoteDao noteDao;
private LiveData<List<Note>> allNotes;
public NoteRepository(Application application) {
NoteDatabase database = NoteDatabase.getInstance(application);
noteDao = database.noteDao();
allNotes = noteDao.getAllNotes();
}
public void insert(Note note) {
new InsertNoteAsyncTask(noteDao).execute(note);
}
public void update(Note note) {
new UpdateNoteAsyncTask(noteDao).execute(note);
}
public void delete(Note note) {
new DeleteNoteAsyncTask(noteDao).execute(note);
}
public void deleteAllNotes() {
new DeleteAllNotesAsyncTask(noteDao).execute();
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private NoteDao noteDao;
private InsertNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note... notes) {
noteDao.insert(notes[0]);
return null;
}
}
private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> { ... //и так далее,
//весь код не буду вставлять
Ответы (1 шт):
Автор решения: Alex Rodionow
→ Ссылка
Разобрался, проблема была во ViewMidelProvider в MainActivity.
Поменял строку на:
noteViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
и все заработало.