无法访问“androidx.lifecycle.HasDefaultViewModelProviderFactory”,它是“FavoriteBottomDialogFragment”的超类型。检查您的模块类别

Ous*_*ine 33 dependencies android classpath bottom-sheet androidx

我在扩展BottomSheetDialogFragment的整个类中出错

Cannot access 'androidx.lifecycle.HasDefaultViewModelProviderFactory' which is a supertype of 'FavoriteBottomDialogFragment'. Check your module classpath for missing or conflicting dependencies
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

该类位于 app 模块中,该模块实现了另外两个模块:core 和presentation-core

构建.gradle

dependencies {

     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation project (':core')
     implementation project (':presentation-core')

     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41"
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.core:core-ktx:1.2.0'
     testImplementation 'junit:junit:4.12'
     implementation "com.google.android.material:material:1.1.0"

     //Rx
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     //Architecture component
     implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
     implementation 'androidx.room:room-runtime:2.0.0'
     kapt 'androidx.room:room-compiler:2.0.0'
     kapt 'androidx.lifecycle:lifecycle-common-java8:2.0.0'
     implementation 'androidx.room:room-rxjava2:2.0.0'
     implementation 'androidx.room:room-guava:2.0.0'

     implementation 'androidx.recyclerview:recyclerview:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
 }
Run Code Online (Sandbox Code Playgroud)

核心依赖

dependencies {
     api fileTree(dir: 'libs', include: ['*.jar'])
     api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61"
     api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
     api 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
     //library to serialize Java Objects between Contexts
     implementation 'org.parceler:parceler-api:1.1.11'
     kapt 'org.parceler:parceler:1.1.11'

     //testing dependencies
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     testImplementation "org.mockito:mockito-core:2.24.5"
     androidTestImplementation "org.mockito:mockito-android:2.24.5"

     //architecture component
     implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
     implementation "androidx.lifecycle:lifecycle-livedata:2.0.0"

     //RxJava2
     implementation "io.reactivex.rxjava2:rxjava:2.2.9"
     implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
}
Run Code Online (Sandbox Code Playgroud)

表示核心

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
}
Run Code Online (Sandbox Code Playgroud)

BottomSheetDialogFragment 在此处输入图片说明

即使出现错误,我也可以在设备中运行该项目

Dav*_*oni 40

我今天遇到了完全相同的问题,并且能够解决。事实证明,问题是androidx.lifecycle:lifecycle-viewmodel“失败”类所在的模块使用的预期版本与其他一些相关代码中的更高版本之间的版本不匹配。

因此,就我而言,我的模块使用的是该模块的2.1.0版,但其中一个依赖项使用的是2.2.0版。代码会编译没有问题,因为gradle解决了对最新版本的依赖;然而,Android Studio 不知何故对这种情况感到困惑(并非总是如此,因为这不会一直发生,但有时——这不是我第一次看到这种情况。)

因此,解决方案是:找出您的应用程序中该库的最新版本,并更新您build.gradle的此模块以指向 gradle 解析到的相同版本。或者:

  1. gradlew app:dependencies
  2. 搜索结果 lifecycle-viewmodel
  3. 更新build.gradle您的应用程序以依赖于lifecycle-viewmodelgradle 说它正在解决的版本
  4. 将项目与 gradle 文件同步


mrt*_*xaz 19

在我的情况下(一个 Android 模块),Android Studio 4.0.1,我在 IDE 中收到了一些与 androidx.lifecycle.HasDefaultViewModelProviderFactory 相关的警告,我通过将这一行添加到 build.gradle 解决了这个问题:

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
Run Code Online (Sandbox Code Playgroud)

因此 build.gradle 的开头变为:

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
...
Run Code Online (Sandbox Code Playgroud)


Yer*_*ray 14

就我而言,问题已解决,在有问题的类的模块中添加生命周期视图模型依赖项。


maX*_*aXp 7

androidx.fragment:fragment-ktx:x.x.x使用实际版本将依赖项添加到您的构建 gradle 中。对于活动,您必须添加androidx.activity:activity-ktx:x.x.x. 我希望这对您有所帮助,就我而言,它解决了问题。