我创建了一个从android视图模型扩展的类,就像我看到的教程视频一样。我写了下面的代码,但有一个错误,我无法解决它我在stackoverflow中搜索,找到了很多答案,但没有一个对我有效我尝试了android视图模型和原始视图模型,也尝试了从android视图模型工厂获取实例,但错误仍然存在
这是我的活动
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) {
Toast.makeText(MainActivity.this, "onChanged()", Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的视图模型
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.deleteAll();
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
这是我的错误
Process: com.amehrvarz.architectureexample, PID: 25944
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amehrvarz.architectureexample/com.amehrvarz.architectureexample.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.amehrvarz.architectureexample.NoteViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.amehrvarz.architectureexample.NoteViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.amehrvarz.architectureexample.MainActivity.onCreate(MainActivity.java:23)
像这样初始化ViewModel。您还需要使用ViewModelProvider
构造函数传递ViewModelFactory
。
noteViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(NoteViewModel.class);
希望这能有所帮助。。。。。。。