【发布时间】:2019-10-18 02:38:42
【问题描述】:
我尝试了 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;
}
}
}
日志
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.InvocationTargetException 在 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.getGeneratedImplementation(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) 应用程序终止。
【问题讨论】:
-
发布整个堆栈跟踪,其中可能包含更多错误。
-
说
Caused by: java.lang.RuntimeException: cannot find implementation for com.prathameshmore.getnotes.database.NoteDatabase. NoteDatabase_Impl does not exist
标签: java android mvvm android-jetpack