我试图在Kotlin中使用匕首2进行依赖注入。我要创建的依赖图是
Application-> Activity -> Fragment
每个组件都提供了必要的绑定/提供程序。这是我当前的结构:
AppModule.kt
@dagger.Module
abstract class AppModule {
@Binds
abstract fun bindContext(application: Application): Context
@Binds
abstract fun bindApplication(application : Application) : Application
}
AppComponent.kt
@Singleton
@Component(modules = arrayOf(AppModule::class,
AndroidInjectionModule::class,
ActivityBuilder::class))
interface AppComponent :
AndroidInjector<FreedomGuardianApp> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<FreedomGuardianApp>()
}
ActivityModule.kt
@Module
abstract class ActivityBuilder {
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class))
abstract fun bindMainActivity() : MainActivity
}
MainActivityModule.kt
@Module(includes =
arrayOf(WatchFaceFragmentModule::class))
class MainActivityModule {
@Provides
fun adapter(activity: MainActivity): MyPagesAdapter {
return MyPagesAdapter(activity.supportFragmentManager)
}
}
WatchFaceFragmentModule.kt
@Module
abstract class WatchFaceFragmentModule {
@PerFragment
@ContributesAndroidInjector
abstract fun contributeWatchFaceFragment(): WatchFaceFragment
}
WatchFaceProvidersModule.kt
@Module
class WatchFaceProvidersModule {
@Provides
fun gestureDetector(fragment: WatchFaceFragment): GestureDetectorCompat {
return GestureDetectorCompat(fragment.context, fragment)
}
@Provides
@Named("textToSpeak")
fun textToSpeak(fragment: WatchFaceFragment): String {
return fragment.resources.getString(R.string.time_string)
}
@Provides
fun textToSpeech(fragment: WatchFaceFragment): TextToSpeech {
return TextToSpeech(fragment.context, fragment)
}
}
我正在WatchFaceFragment中注入GestureDetectorCompat,String(textToSpeak),TextToSpeak。
这是错误:
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.medicalguardian.freedomguardian.FreedomGuardianApp> {
^
A binding with matching key exists in component: com.medicalguardian.freedomguardian.di.WatchFaceFragmentModule_ContributeWatchFaceFragment.WatchFaceFragmentSubcomponent
com.medicalguardian.freedomguardian.ui.fragment.WatchFaceFragment is injected at
com.medicalguardian.freedomguardian.ui.MainActivity.watchFaceFragment
com.medicalguardian.freedomguardian.ui.MainActivity is injected at
dagger.android.AndroidInjector.inject(T) [com.medicalguardian.freedomguardian.di.AppComponent ? com.medicalguardian.freedomguardian.di.ActivityBuilder_BindMainActivity.MainActivitySubcomponent]
我的结构错了吗?我没有注入WatchFaceFragment的构造函数,因为它没有帮助。我也使用了Dagger支持(DaggerFragment,DaggerApplication等)。如果我不是注入片段而是仅注入MainActivity,那就很好。我该如何解决这个问题?冗长的阅读提前致歉。任何帮助表示赞赏。
答案 0 :(得分:0)
您尚未向我们显示要在其中包含WatchFaceProvidersModule的位置,但是它应该 包含在WatchFaceFragmentModule.kt的@ContributesAndroidInjector
批注中,而不是包含在AppComponent或ActivityModule中:>
@Module
abstract class WatchFaceFragmentModule {
@PerFragment
@ContributesAndroidInjector(modules = arrayOf(WatchFaceProvidersModule::class))
abstract fun contributeWatchFaceFragment(): WatchFaceFragment
}
这应该确保在注入WatchFaceFragment实例(以及所需的GestureCompat等)时,您可以访问WatchFaceFragment实例。否则,如评论中提到的David所示,您的FreedomGuardianApp或MainActivity的注入器将不知道返回哪个WatchFaceFragment。一个可能还不存在。