Reputation: 39
I try creating a view model of a youtube tutorial, that's supposed to show all the entries from a database (but at this point it shoudl only display a Toast as there's no Recycler View yet)
I get the following exception when opening the activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial/com.example.tutorial.Activity2}: java.lang.RuntimeException: Cannot create an instance of class com.example.tutorial.EventViewModel
......
at com.example.tutorial.Activity2.onCreate(Activity2.java:23)
Class looks as following:
public class Activity2 extends AppCompatActivity {
private EventViewModel eventViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
eventViewModel = new ViewModelProvider(this).get(EventViewModel.class);
eventViewModel.getAllEvents().observe(this, new Observer<List<Event>>() {
@Override
public void onChanged(List<Event> events) {
Toast.makeText(Activity2.this, "thhthth", Toast.LENGTH_SHORT).show();
}
});
}
}
ViewModel Class:
public class EventViewModel extends AndroidViewModel {
private EventRepository repository;
private LiveData<List<Event>> allEvents;
public EventViewModel(@NonNull Application application) {
super(application);
repository = new EventRepository(application);
allEvents = repository.getAllEvents();
}
public void insert(Event event) {
repository.insert(event);
}
public void update(Event event) {
repository.update(event);
}
public void delete(Event event) {
repository.delete(event);
}
public LiveData<List<Event>> getAllEvents() {
return allEvents;
}
}
How can I fix this? (still an Android beginner btw :^) )
Thanks in advance!
Upvotes: 1
Views: 750
Reputation: 1
https://stackoverflow.com/a/60857926/13614001
Android Studio - version 3.6.2
This worked for me.
Make sure you include the dependencies in the build.gradle(Module:app) and as well as the code changes.
More importantly "do the grade sync" after including the dependencies.
Upvotes: 0
Reputation: 391
because "ViewModelProviders" is deprecated, for java programmers, use this:100% gonna work
@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, "Onchange ", Toast.LENGTH_SHORT).show();
}
});
Go to your build.gradle(Module:app)
def lifecycle_version = "2.2.0"
def room_version = "2.2.5"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
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 Activity2
) and put this line:
eventViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(EventViewModel.class);
So your code should look like this:
public class Activity2 extends AppCompatActivity {
private EventViewModel eventViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
eventViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(EventViewModel.class);
eventViewModel.getAllEvents().observe(this, new Observer<List<Event>>() {
@Override
public void onChanged(List<Event> events) {
Toast.makeText(Activity2.this, "enjoy :)", Toast.LENGTH_SHORT).show();
}
});
}
}
3.Run your app.
Upvotes: 4
Reputation: 140
You must define "eventViewModel" in method onCreate and don't make a general variable.
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
EventViewModel eventViewModel = new ViewModelProvider(this).get(EventViewModel.class);
...
}
}
Upvotes: 1
Reputation: 11
Your viewModel extends AndroidViewModel. Use AndroidViewModelFactory:
yourViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(YourViewModel.class);
Upvotes: 1