gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:25:59 26 4
gpt4 key购买 nike

我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com