android - 为什么 AdnroidInjector.inject(fragment) 使用已弃用的 android.support.v4.app.Fragment

标签 android dagger-2 android-viewmodel androidinjector

我在 Android 应用程序中使用 Dagger2 作为 DI,

我想通过 AndroidInjector 在 fragment 中注入(inject) viewModel 并有这两行:

 AndroidInjection.inject(this)
 viewModel = ViewModelProviders.of(this, viewModelFactory).get(ProductDetailViewModel::class.java)

当我的 fragment 扩展 androidx.fragment.app.Fragment 时显示此错误:

  • 第一行使用了弃用的android.support.v4.app.Fragment
  • 但第二个使用 android.app.Fragment

现在我不知道我的 fragment 应该扩展哪一个!

我在选择其中一个时出现此错误:

不能使用提供的参数调用以下函数: public open fun inject(activity: Activity!): 单元定义在dagger.android.AndroidInjection public open fun inject(fragment: Fragment!): 单元定义在dagger.android.AndroidInjection public open fun inject(service: Service!): 单元定义在dagger.android.AndroidInjection public open fun inject(contentProvider: ContentProvider!): 单元定义在dagger.android.AndroidInjection

最佳答案

我的错误是使用了 dagger-android(不是 dagger-android-support),而 dagger-android 只有:

 AndroidInjection.inject(this)

但我应该使用

 AndroidSupportInjection.inject(this)

那是在名为 dagger-android-support

的 dagger-android 的 supportLibrary 版本中

关于android - 为什么 AdnroidInjector.inject(fragment) 使用已弃用的 android.support.v4.app.Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868704/

相关文章:

android - 发现多个文件的操作系统独立路径为 'META-INF/INDEX.LIST' (Android/Gradle)

android - 如何防止移动设备调整大小和使用正确的 css 文件

android - 从 Android Studio 的全局搜索中删除 Dagger 生成的类?

android - MutableLiveData<Pair<Float, Float>>() -> Val 不能被重新赋值

python - 在具有多个参数的 Tornado HttpGet 中传递一个参数

java - Dagger2 无法生成组件类

android - 运行什么任务来重新创建 Dagger 2 注入(inject)文件

android - viewModelScope 未解析的引用

android - 每个屏幕有多个 View 模型

android - 当 NestedScrollView 为空时停止 AppBarLayout 滚动屏幕