https://kotlinlang.org logo
#android
Title
# android
d

deactivateduser

12/15/2019, 9:23 AM
Hi. I'm not able to instantiate the ViewModel. I'm getting the above error.
d

dawidhyzy

12/15/2019, 10:05 AM
This is wrong place to ask. Your question is not specific to Kotlin. Read this first: https://developer.android.com/topic/libraries/architecture
d

deactivateduser

12/15/2019, 11:11 AM
Thank you for your reply. I don't get this error in Java. Only in Kotlin. Could you please help?
t

tseisel

12/15/2019, 5:20 PM
I really recommend that you read the Android Architecture Guide : your code has some serious issues, be it in Java or Kotlin. Android
ViewModel
should never keep a reference to a View, Fragment or Activity. Your crash is due to the fact that
CardViewModel
has a constructor parameter, so the framework is unable to instantiate it (only no-args constructors are allowed, unless you specify a factory). Check your implementation of
ViewModelProvider.Factory
for
CardViewModel
.
👍 3
d

deactivateduser

12/16/2019, 9:18 AM
@tseisel Thank you so much. I really missed the first part of ViewModel. I have read about it. Now I understand how it's actually works. Thanks again. ✌🏼
8 Views