【发布时间】:2019-09-03 23:31:26
【问题描述】:
我遇到了这个问题:
ApplicationComponent.java:8: error: [Dagger/MissingBinding] @... java.text.SimpleDateFormat 不能在没有@Provides-annotated 方法的情况下提供。
模块
@Module
abstract class ApplicationModule {
@Binds
@AppContext
abstract fun application(app: App): Context
@Module
companion object {
...
@Provides
@Singleton
@CalendarPickerDateFormat
fun provideCalendarPickerDateFormat(): SimpleDateFormat {
return SimpleDateFormat("dd/MMM/yyyy", Locale.getDefault())
}
}
}
限定符
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class CalendarPickerDateFormat
类
@ActivityScope
class MyClass
@Inject constructor(
...,
@CalendarPickerDateFormat private val calendarDateFormat: SimpleDateFormat
) {...}
即使我将 @Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) 添加到 Qualifier 并将类构造函数更改为 @param:CalendarPickerDateFormat,我也会收到相同的错误。
缺少什么?
可能的解决方案
添加@JvmStatic 喜欢:
@Provides
@Singleton
@JvmStatic
@CalendarPickerDateFormat
fun provideCalendarPickerDateFormat(): SimpleDateFormat {
return SimpleDateFormat("dd/MMM/yyyy", Locale.getDefault())
}
解决构造函数注入但不解决字段注入:
@Inject
@CalendarPickerDateFormat lateinit var date : SimpleDateFormat
为什么?
注意:我也尝试过@Module object class approach,但结果相同。
【问题讨论】:
标签: android kotlin dependency-injection dagger-2