Cannot resolve ViewModelProvider construction in a fragment?

后端 未结 4 1819
悲哀的现实
悲哀的现实 2021-02-19 05:28

I have been spending a lot of time trying to figure out why in the code below (towards the end), I get an error on ViewModelProvider(this). I also tried getActivity() instead of

4条回答
  •  遥遥无期
    2021-02-19 05:56

    You aren't using the latest library release in which the ViewModelProvider(@NonNull ViewModelStoreOwner owner) constructor was included. You are seeing the latest docs but not using the latest library version of ViewModel. You need to use

        implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0' // For Java
    

    or

        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' // For kotlin extension
    

提交回复
热议问题