我尝试了 MVVM 架构,实现了所有必需的类和方法。在 MainActivity 中创建 ViewModel 类的对象时,出现此错误java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel
.
我尝试了 YouTube 教程中的这个示例。我正确地执行了所有操作。我尝试将 ViewModel 类和构造函数公开,但应用程序仍然在运行时崩溃。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
Toast.makeText(MainActivity.this, "Updated", Toast.LENGTH_SHORT).show();
}
});
}
}
NoteViewModel.java
public class NoteViewModel extends AndroidViewModel {
private NoteRepository noteRepository;
private LiveData<List<Note>> allNotes;
public NoteViewModel(@NonNull Application application) {
super(application);
noteRepository = new NoteRepository(application);
allNotes = noteRepository.getAllNotes();
}
public void insert(Note note) {
noteRepository.insert(note);
}
public void update(Note note) {
noteRepository.update(note);
}
public void delete(Note note) {
noteRepository.delete(note);
}
public void deleteAllNotes() {
noteRepository.deleteAllNotes();
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
NoteRepository.java
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 delete(Note note) {
new DeleteNoteAsyncTask(noteDao).execute(note);
}
public void update(Note note) {
new UpdateNoteAsyncTask(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> {
private NoteDao noteDao;
private UpdateNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note...notes) {
noteDao.update(notes[0]);
return null;
}
}
private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private NoteDao noteDao;
private DeleteNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note...notes) {
noteDao.delete(notes[0]);
return null;
}
}
private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {
private NoteDao noteDao;
private DeleteAllNotesAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Void...voids) {
noteDao.deleteAllNotes();
return null;
}
}
}
Log
E/AndroidRuntime:致命异常:main
进程:com.prathameshmore.getnotes,PID:28833
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.prathameshmore.getnotes/com.prathameshmore.getnotes.views.MainActivity}:
java.lang.RuntimeException:无法创建类的实例
com.prathameshmore.getnotes.viewmodel.NoteViewModel
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:163)
在 android.app.ActivityThread.main(ActivityThread.java:6238)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
导致:java.lang.RuntimeException:无法创建类 com.prathameshmore.getnotes.viewmodel.NoteViewModel 的实例
在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:208)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103)
在 com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25)
在 android.app.Activity.performCreate(Activity.java:6868)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:163)
在 android.app.ActivityThread.main(ActivityThread.java:6238)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
引起原因:java.lang.reflect.InitationTargetException
在 java.lang.reflect.Constructor.newInstance0(本机方法)
在 java.lang.reflect.Constructor.newInstance(Constructor.java:430)
在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:200)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103)
在 com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25)
在 android.app.Activity.performCreate(Activity.java:6868)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:163)
在 android.app.ActivityThread.main(ActivityThread.java:6238)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
原因:java.lang.RuntimeException:找不到 com.prathameshmore.getnotes.database.NoteDatabase 的实现。
注意Database_Impl不存在
在 androidx.room.Room.getGenerateImplementation(Room.java:94)
在 androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:851)
在 com.prathameshmore.getnotes.database.NoteDatabase.getInstance(NoteDatabase.java:31)
在 com.prathameshmore.getnotes.repository.NoteRepository。(NoteRepository.java:20)
在 com.prathameshmore.getnotes.viewmodel.NoteViewModel.(NoteViewModel.java:21)
在 java.lang.reflect.Constructor.newInstance0(本机方法)
在 java.lang.reflect.Constructor.newInstance(Constructor.java:430)
在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:200)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135)
在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103)
在 com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25)
在 android.app.Activity.performCreate(Activity.java:6868)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:163)
在 android.app.ActivityThread.main(ActivityThread.java:6238)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
申请终止。