Cannot Create Instance Of Class ViewModel

后端 未结 3 1256
情歌与酒
情歌与酒 2020-12-22 00:43

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

3条回答
  •  时光说笑
    2020-12-22 00:55

    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'
    

提交回复
热议问题