ViewModel in Kotlin: Unresolved Reference

前端 未结 7 1930
离开以前
离开以前 2021-01-07 17:24

I am trying to implement ViewModel in a 100% Kotlin app. Every piece of documentation I can find says I want to use this to get the ViewModel instance:

ViewM         


        
相关标签:
7条回答
  • 2021-01-07 18:01

    This can also be resolved by targeting minSdkVersion to 21.

    If you have minSdkVersion 21 or higher, you won't need implementation "android.arch.lifecycle:extensions:1.1.1".

    0 讨论(0)
  • 2021-01-07 18:03

    Include the following as a dependency:

    implementation "android.arch.lifecycle:extensions:1.1.1"
    

    This dependency is for both ViewModel and LiveData and thus would not require you to give separate dependencies for the same either; i.e. the first two dependencies indicated by you can be replaced by the aforementioned lifecycle extensions dependency.

    0 讨论(0)
  • 2021-01-07 18:08
    android {
        kotlinOptions {
            jvmTarget = '1.8'
        } 
    }
    

    project e app level

    0 讨论(0)
  • 2021-01-07 18:14

    I faced this kind of problem in AndroidStudio 3.0.1 and solved by adding following dependencies in the proper build.gradle:

    implementation "android.arch.lifecycle:extensions:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
    

    version code may be different as AndroidStudio tells you if it differs.

    0 讨论(0)
  • 2021-01-07 18:19

    In my case I was missing :

    implementation "androidx.fragment:fragment-ktx:1.1.0"
    
    0 讨论(0)
  • 2021-01-07 18:19

    If the above "adding/updating dependecies" did not resolve your issue then try to restart your android studio. It´s just to root, I do not see any major issue with it. Hope it helps.

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