English 中文(简体)
未解析的引用视图Models()Hilt
原标题:Unresolved reference viewModels() Hilt

我已经实现了互联网上提供的所有依赖,但仍然无法解决这个问题。refered这个链接和stackoverflow上的其他链接,但无法解决问题。

我在活动上创建viewModel实例时遇到了问题,在片段中我没有遇到任何挑战。

以下是我的应用程序构建等级:

plugins {
    id  com.android.application 
    id  org.jetbrains.kotlin.android 
    id  kotlin-android 
    id  kotlin-parcelize 
    id  androidx.navigation.safeargs 
    id  kotlin-kapt 
    id  com.google.dagger.hilt.android 
}
dependencies {

    implementation  androidx.core:core-ktx:1.10.1 
    implementation  androidx.appcompat:appcompat:1.6.1 
    implementation  com.google.android.material:material:1.9.0 
    implementation  androidx.constraintlayout:constraintlayout:2.1.4 

    implementation  androidx.navigation:navigation-fragment-ktx:2.6.0 
    implementation  androidx.navigation:navigation-ui-ktx:2.6.0 

    implementation  androidx.fragment:fragment-ktx:1.6.1 
    //Retrofit - network calls
    implementation  com.squareup.retrofit2:retrofit:2.9.0 
    implementation  com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.6 
    implementation  com.squareup.retrofit2:converter-gson:2.9.0 
    implementation  com.google.code.gson:gson:2.9.0 
    implementation  com.google.android.gms:play-services-location:21.0.1 
    implementation  com.google.firebase:firebase-messaging:23.2.0 

    //Hilt - Dependency Injection
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-android-compiler:2.44")


    //Page lifecycle owners
    def lifecycle_version = "2.6.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "android.arch.lifecycle:extensions:1.1.1"

    def coroutines_version = "1.6.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    //QR generation library
    implementation  com.google.zxing:core:3.4.1 

 
    testImplementation  junit:junit:4.13.2 
    androidTestImplementation  androidx.test.ext:junit:1.1.5 
    androidTestImplementation  androidx.test.espresso:espresso-core:3.5.1 

}

Project build gradle :
plugins {
    id  com.android.application  version  7.4.1  apply false
    id  com.android.library  version  7.4.1  apply false
    id  org.jetbrains.kotlin.android  version  1.8.0  apply false
    id  androidx.navigation.safeargs.kotlin  version  2.5.1  apply false
    id("com.google.dagger.hilt.android") version "2.44" apply false
}

Code in activity : enter image description here

Code in Fragment : enter image description here

提前谢谢。

问题回答

您需要添加活动ktx依赖项,如下所示:

implementation  androidx.activity:activity-ktx:1.6.1 , //you can change the version as needed/




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签