Why am i getting this error when trying to Implement ViewModel?

Viewed 29

I am trying to learn about ViewModels so i am following the instructions from the Android Developer docs, but unfortunately I am receiving this message i attached a screenshot. All I have really done so far was added necessary implementations stated on the website and followed what was stated on the doc to implement it.

  def lifecycle_version = "2.6.0-alpha02"
    def arch_version = "2.1.0"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // Lifecycles only (without ViewModel or LiveData)
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    //Was not recognizing ViewModelProvider so i added this to fix it..
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"

enter image description here

1 Answers

Add second parameter ViewModelFactory in ViewModelProvider

val factory = YourViewModelFactory(YourRepository())     
viewModel = ViewModelProvider(this, factory).get(yourViewModelclass::class.java)
Related