【问题标题】:use View Model Providers(ViewModelProviders) in Dagger Dialog Fragment在 Dagger Dialog Fragment 中使用 View Model Providers(ViewModelProviders)
【发布时间】:2019-02-27 18:56:18
【问题描述】:

当我在 Dagger Fragment 中使用 ViewModelProvider.of 时,我可以毫无问题地使用它,但是在 Dagger Dialg Fragment 中,我在第一个参数(片段)中出现错误

在代码中

viewModel = ViewModelProviders.of(this, viewModelFactory).get(CreatePlanViewModel::class.java)

错误

以下函数都不能使用提供的参数调用。

of(Fragment,ViewmodelProvider.Factory)

of(FragmentActivity,VIewModelProvider.Factory)

注意 我使用了活动,但没有工作

【问题讨论】:

    标签: android android-fragments viewmodel dagger-2


    【解决方案1】:

    确保您使用的是支持库中的DialogFragment

    应该导入android.support.v4.app.DialogFragment 而不是android.app.DialogFragment,因为ViewModelProviders.of() expects a Fragment from the support library

    【讨论】:

    • 我正在使用来自 androidX 的片段,没有错误也许我应该创建一个类并启用 HasSupportFragmentInjector
    • 你可以使用 DaggerDialogFragment
    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2019-01-22
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多