没有将喷油器工厂绑定到Class< [英] No injector factory bound for Class<>

查看:256
本文介绍了没有将喷油器工厂绑定到Class<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在堆栈中看到了相同的问题,但是他们尝试使用@ContributesAndroidInjector修复错误,但是在Dagger文档中说使用@ContributesAndroidInjector仅是可选的,所以这是我的类:

I saw the same question in stack but they try to fix the error with the @ContributesAndroidInjector but in Dagger documentation says use @ContributesAndroidInjector is only optional, so here are my classes:

我的MainActivityComponent:

@Subcomponent(modules = [
    MainBuilder::class
])
@ActivityScope
interface MainComponent: AndroidInjector<MainActivity>{

    @Subcomponent.Factory
    interface Factory: AndroidInjector.Factory<MainActivity>
}

我的AplicationBinder:

@Module(subcomponents = [
    MainComponent::class
])
abstract class AppBuilder {

    @Binds
    @IntoMap
    @ClassKey(MainActivity::class)
    abstract fun mainActivityFactoryBind(factory: MainComponent.Factory): AndroidInjector.Factory<out Activity>

}

还有扩展我的MainActivity的我的BaseActivity:

abstract class BaseActivity: HasSupportFragmentInjector, AppCompatActivity() {

    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
    }

    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return dispatchingAndroidInjector
    }
}

我该如何解决这个问题?

How i can solve this issue?

推荐答案

实际上,匕首文档说:

提示:如果您的子组件及其工厂没有其他方法,或者 除了步骤2中提到的超类型之外,您可以使用 @ContributesAndroidInjector为您生成它们.代替步骤 2和3,添加一个抽象模块方法来返回您的活动, 使用@ContributesAndroidInjector对其进行注释,并指定模块 您要安装到子组件中.如果子组件需要 范围,请将范围注释也应用到该方法.

Pro-tip: If your subcomponent and its factory have no other methods or supertypes other than the ones mentioned in step #2, you can use @ContributesAndroidInjector to generate them for you. Instead of steps 2 and 3, add an abstract module method that returns your activity, annotate it with @ContributesAndroidInjector, and specify the modules you want to install into the subcomponent. If the subcomponent needs scopes, apply the scope annotations to the method as well.

因此,基本上@ContributesAndroidInjector会生成您正在手动执行的子组件操作.由于您的案子与此步骤中的Daggers文档相匹配,因此您可以自由使用@ContributesAndroidInjector.

So basically the @ContributesAndroidInjector will generate that subcomponent thing you are doing manually. Since your case matches the Daggers documentation on this step, you can freely use @ContributesAndroidInjector.

示例:

@Singleton
@Component(
    modules = [AndroidInjectionModule::class, ActivityModule::class, BroadCastReceiversModule::class,...]
)
interface AppComponent {
    fun inject(pocketTreasureApplication: MyApplication)

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance application: Application): AppComponent
    }
}

AndroidInjectionModule没有Dagger.在这种情况下,它告诉Dagger:嘿,我们有要处理的Android组件,然后Dagger知道如何生成它们.

The AndroidInjectionModule is free from Dagger. In that case it tells Dagger: Hey we have Android components to deal with and than Dagger knows how to generate them.

然后,您应该使用模块,例如ActivityModule来生成扩展ActivitiesFragmentsServicesBroadCastReceivers等的类.

Than, you should use your modules, likeActivityModule to generate your classes that extend Activities, Fragments, Services, BroadCastReceivers etc.

所以ActivityModule hold the @ ContributesAndroidInjector`:

So the ActivityModule hold the@ContributesAndroidInjector`:

    @Singleton
    @ContributesAndroidInjector(modules = [FragmentModule::class])
    abstract fun contributeMainactivity(): MainActivity

现在的Dagger知道您可以神奇地注入对MainActivity的依赖关系. 里面的FragmentModule同样适用.

And the now Dagger knows that you may magically inject dependencies on MainActivity. Same works for the FragmentModule inside it.

比在MainActivity中,您可以:

AndroidInjection.inject(this)并注入您的依赖项.

AndroidInjection.inject(this) and inject your dependencies.

仅此而已.您可以在我的Dagger-Android个人文章中查看更多内容

That's all. You may check more into my personal article for Dagger-Android here.

这篇关于没有将喷油器工厂绑定到Class&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆