Reputation: 1489
I trying hands on MVVM architecture, I implemented all required classes and methods. While creating an object of ViewModel class in MainActivity, I got this error java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel
.
I trying this example from a YouTube tutorial. I did all the implementation correct. I tried on making ViewModel class and constructor public but still, app crashing at runtime.
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: FATAL EXCEPTION: main Process: com.prathameshmore.getnotes, PID: 28833 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prathameshmore.getnotes/com.prathameshmore.getnotes.views.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.RuntimeException: Cannot create an instance of class com.prathameshmore.getnotes.viewmodel.NoteViewModel at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:208) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) at com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6868) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:200) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) at com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6868) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at 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 at androidx.room.Room.getGeneratedImplementation(Room.java:94) at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:851) at com.prathameshmore.getnotes.database.NoteDatabase.getInstance(NoteDatabase.java:31) at com.prathameshmore.getnotes.repository.NoteRepository.(NoteRepository.java:20) at com.prathameshmore.getnotes.viewmodel.NoteViewModel.(NoteViewModel.java:21) at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:200) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) at com.prathameshmore.getnotes.views.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6868) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6238) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) Application terminated.
Upvotes: 2
Views: 5503
Reputation: 1855
In my case I accidently put this in project gradle which prevent or exculded the lifecycle dependencies and viewmodel from the project. Remove it
allprojects {
configurations.all {
resolutionStrategy.force "com.android.support:support-v4:26.1.0"
exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
}
}
Upvotes: 0
Reputation: 555
Put this
noteViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
instead of
new ViewModelProvider(this).get(mainActivityViewModel.class);
Upvotes: 3
Reputation: 1413
Considering to CBS's answer as with 2.3.0-alpha01 update lifecycle-extensions artifact is no longer published.
Dependencies
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.fragment:fragment-ktx:1.2.4"
AndroidViewModel Class
class TestVM (app : android.app.Application) : AndroidViewModel(app){
//Code
}
In Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel = ViewModelProvider(this).get(TestVM::class.java)
}
Upvotes: 1
Reputation: 126
This would work fine:
1.Go to your build.gradle(Module:app) and add this in the dependencies :
`implementation "android.arch.lifecycle:extensions:$lifecycle_version"`
make sure to have the def lifecycle_version = "2.2.0"
in your dependencies.
So it shoud look like this:
dependencies {
def lifecycle_version = "2.2.0" //make sure to have this
//Some implementations ...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "android.arch.lifecycle:extensions:$lifecycle_version" // make sure to have this too.
}
2.Go to your Activity (in your case it's the MainActivity
) and put this line:
noteViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
So your MainActivity.java
should look like this:
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, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
Toast.makeText(MainActivity.this, "Update, Enjoy coding :)", Toast.LENGTH_SHORT).show();
}
});
}
}
3.Run your app.
Upvotes: 3
Reputation: 6073
You need to have a factory class for your ViewModel
.
public class MyViewModelFactory implements ViewModelProvider.Factory {
private Application mApplication;
private String mParam;
public MyViewModelFactory(Application application, String param) {
mApplication = application;
mParam = param;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new MyViewModel(mApplication, mParam);
}
}
And when instantiating the view model, you do like this:
MyViewModel myViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication(), "my awesome param")).get(MyViewModel.class);
Upvotes: 3