I\'m currently working on a countdown app, and whenever I open it, it fails with a message saying Cannot create instance of class com.nailuj29gaming.CountdownViewModel
You need to upgrade to Fragment 1.2.0 or higher, which is what adds a support for the ViewModelProvider
constructor as per the Lifecycle 2.2.0 release notes:
You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality when using Fragment 1.2.0.
When using an older version of Fragments (i.e., the Fragment 1.1.0 that AppCompat 1.1.0 pulls in), you'll only get the ViewModelProvider$NewInstanceFactory
you see in your exception trace, rather than a ViewModel.Factory that supports AndroidViewModel
.
Therefore you should add an explicit dependency on Fragment 1.2.1 (the current latest):
implementation 'androidx.fragment:fragment:1.2.1'