没有将喷油器工厂绑定到Class< [英] No injector factory bound for 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
来生成扩展Activities
,Fragments
,Services
,BroadCastReceivers
等的类.
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<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!