gpt4 book ai didi

android - 带有 Hilt 的 Dagger 从模块中的适配器中注入(inject) ActivityContext

转载 作者:行者123 更新时间:2023-12-02 12:34:28 55 4
gpt4 key购买 nike

我正在使用 Dagger 和刀柄,我想将 @ActivityContext 从模块注入(inject)到适配器,但我收到了这个错误 -

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1/mnt/My Projects/app/build/generated/source/kapt/debug/app/myapp/MyApp_HiltComponents.java:156: error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.
public abstract static class ApplicationC implements MyApp_GeneratedInjector,
^
@dagger.hilt.android.qualifiers.ActivityContext android.content.Context is injected at
app.myapp.di.modules.activitiesModules.HomeActivityModule.provideAdapterFragmentState(context)
app.myapp.ui.base.AdapterFragmentState is injected at
app.myapp.ui.home.HomeActivity.adapterFragmentState
app.myapp.ui.home.HomeActivity is injected at
这是我的 HomeActivityModule -
@Module
@InstallIn(ActivityRetainedComponent::class)
object HomeActivityModule {

@Provides
@ActivityRetainedScoped
fun provideAdapterFragmentState(@ActivityContext context: Context): AdapterFragmentState {
return AdapterFragmentState(context)
}

}
这是我的适配器 -
@ActivityRetainedScoped
class AdapterFragmentState @Inject constructor(@ActivityContext context: Context)
: FragmentStateAdapter(context as AppCompatActivity){
哪一部分是错的?

最佳答案

@Module
@InstallIn(ActivityComponent::class)
object HomeActivityModule {

@Provides
@ActivityScoped
fun provideAdapterFragmentState(@ActivityContext context: Context): AdapterFragmentState {
return AdapterFragmentState(context)
}

}
编辑:
因为 ActivityRetainedComponent 存在于配置更改中,而 ActivityComponent 没有。
如果你想注入(inject)@ActivityContext,你的模块应该安装在ActivityComponent中,并且注入(inject)的对象必须是@ActivityScoped。

关于android - 带有 Hilt 的 Dagger 从模块中的适配器中注入(inject) ActivityContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62850811/

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