Cannot create an instance of class ViewModel

后端 未结 23 2429
[愿得一人]
[愿得一人] 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 12:57

    I had this problem following google's roomdb CodeLab. Solution was changing the following.

    Edited

    Add the following Build dependencies to Gradle file (as of 2/22/2020)

    implementation 'androidx.fragment:fragment:1.2.2'
    implementation 'androidx.lifecycle:lifecycle-process:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-service:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'
    

    Imports within the fragment

    import androidx.lifecycle.ViewModelProvider;
    import androidx.fragment.app.Fragment;
    import androidx.lifecycle.Observer;
    

    Creating the viewModel. Add one of the following methods.

    Note: I'v seen this done many ways. I believe the correct way is using getDefaultViewModelProviderFactory(). But I have been using requireActivity().

     new ViewModelProvider(requireActivity(),getDefaultViewModelProviderFactory()).get(YourViewModel.class);
    

    |

     new ViewModelProvider(requireActivity()).get(YourViewModel.class);
    
              
    

    ViewModelProvider Docs

    Deprecated

    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-rc01'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0-rc01'
    
    0 讨论(0)
  • 2020-12-05 12:58

    If you are using Hilt then don't forget to add these four dependencies.

        implementation "com.google.dagger:hilt-android:2.28-alpha"
        kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
        implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
        kapt "androidx.hilt:hilt-compiler:1.0.0-alpha01"
    

    Note:- If any of these dependencies are missing you will get Cannot create an instance of class ViewModel error

    0 讨论(0)
  • 2020-12-05 12:58

    I'm a proficient Android developer and I have used ViewModel 100s of times with no issue. Today I came across this issue. Spent hours and scrolled through various SO posts. Didn't get solved.

    Then I saw that the package name in which I have the ViewModel contains new. Like this:

    com.myapp.myfeature.new.feature

    I changed new to neww for testing like this: com.myapp.myfeature.neww.feature

    and it worked! I hope someone find it useful.

    0 讨论(0)
  • 2020-12-05 12:59

    Please add below code. It worked for me

    val binding = FragmentLayoutBinding.inflate(inflater, container, false)
    
    val viewModel = ViewModelProvider(
                requireActivity(),
                defaultViewModelProviderFactory
                ).get(MainViewModel::class.java)
    
    0 讨论(0)
  • 2020-12-05 13:00

    Make ViewModal class and constructure public

    0 讨论(0)
  • 2020-12-05 13:04

    Make your constructor public.

    0 讨论(0)
提交回复
热议问题