I am trying to write a sample app using Android architecture components and but even after trying for days I could not get it to work. It gives me the above exception.
If constructor of your viewmodel is public and accepts only application then make sure you can create your own model without ViewModelProvider
. Error message might be much more clear:
val model = YouViewModel(app)
Make sure your ViewModel
has constructor with only one parameter i.e. Application
.
example:
public YourViewModel(Application application) {
super(application);
...
Make the class and constructor public it solved my problem .
If you are using Kotlin
make sure to replace any annotationProcessor
in build.gradle
with kapt
.
Like:
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
Will become
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
and add
apply plugin: 'kotlin-kapt'
on top of the buidl.gradle
file.
Annotation Processing with Kotlin
if your PostViewModel class is an inner class, make sure its public and static