Cannot create an instance of class ViewModel

后端 未结 23 2674
[愿得一人]
[愿得一人] 2020-12-05 12:37

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.

23条回答
  •  抹茶落季
    2020-12-05 13:14

    There are few reason to throw the exception . I have mention some of them..

    1. Make sure your view Model class is public
    2. Make sure your view model class constructor is public

    3. Make sure you have added the dependency in your gridle file for lifecycle also if you use room and other libries you have added ..

    4. if you create object any other dependent class in your view model class constructor . Other class can throw error to create the instance of viewModel

提交回复
热议问题