如何修复科特林中的“[Dagger/MissingBinding]”?
How to fix "[Dagger/MissingBinding]" in kotlin?
我正在尝试在 AppComponent 中注入 LatestChart 并解决 [Dagger/MissingBinding] 没有 @Inject 构造函数或 @Provides-annotated 方法的情况下无法提供 LatestChart 的问题...
提前感谢您的帮助
LatestChart.kt
class LatestChart {
@Inject
lateinit var context: Context
@Inject
lateinit var formatter: YearValueFormatter
lateinit var chart: LineChart
init {
App.appComponent.inject(this)
}
fun initChart(chart: LineChart) {
this.chart = chart
***
}
fun addEntry(value: Float, date: Float) {
***
}
}
private fun createSet(): LineDataSet {
***
}
return set
}
}
和AppComponent.kt
@Component(modules = arrayOf(AppModule::class, RestModule::class, MvpModule::class, ChartModule::class))
@Singleton
interface AppComponent {
***
fun inject(chart: LatestChart)
}
编译错误:
***AppComponent.java:8: error: [Dagger/MissingBinding] sn0w.test.crypto.chart.LatestChart cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent {
^
A binding with matching key exists in component: sn0w.test.crypto.di.AppComponent
sn0w.test.crypto.chart.LatestChart is injected at
sn0w.test.crypto.activities.ChartActivity.latestChart
sn0w.test.crypto.activities.ChartActivity is injected at
sn0w.test.crypto.di.AppComponent.inject(sn0w.test.crypto.activities.ChartActivity)
您当前尝试将依赖项注入 LatestChart
的方式是如何满足 Dagger 不会创建的对象(例如活动)中的依赖项。如果您自己创建 LatestChart
对象(只是 val latestCharts = LatestCharts()
),您会看到 context
和 formatter
实际上被注入到这个新创建的实例中。
然而,如果你想让 Dagger 将一个 LatestChart
对象注入另一个对象(根据编译错误我假设是 ChartActivity
),你必须让 Dagger 知道如何创建它的实例。有两种方法可以实现:
1.用 @Inject
注释 LatestChart
构造函数
class LatestChart @Inject constructor(
private val context: Context,
private val formatter: YearValueFormatter
) {
lateinit var chart: LineChart
fun initChart(chart: LineChart) { ... }
fun addEntry(value: Float, date: Float) { ... }
private fun createSet(): LineDataSet { ... }
}
2。在 Dagger 模块之一中创建提供程序方法
class LatestChart(private val context: Context, private val formatter: YearValueFormatter) {
lateinit var chart: LineChart
fun initChart(chart: LineChart) { ... }
fun addEntry(value: Float, date: Float) { ... }
private fun createSet(): LineDataSet { ... }
}
@Module
class LatestChartModule {
@Module
companion object {
@JvmStatic
@Provides
fun provideLatestChart(context: Context, formatter: YearValueFormatter): LatestChart =
LatestChart(context, formatter)
}
}
我正在尝试在 AppComponent 中注入 LatestChart 并解决 [Dagger/MissingBinding] 没有 @Inject 构造函数或 @Provides-annotated 方法的情况下无法提供 LatestChart 的问题... 提前感谢您的帮助
LatestChart.kt
class LatestChart {
@Inject
lateinit var context: Context
@Inject
lateinit var formatter: YearValueFormatter
lateinit var chart: LineChart
init {
App.appComponent.inject(this)
}
fun initChart(chart: LineChart) {
this.chart = chart
***
}
fun addEntry(value: Float, date: Float) {
***
}
}
private fun createSet(): LineDataSet {
***
}
return set
}
}
和AppComponent.kt
@Component(modules = arrayOf(AppModule::class, RestModule::class, MvpModule::class, ChartModule::class))
@Singleton
interface AppComponent {
***
fun inject(chart: LatestChart)
}
编译错误:
***AppComponent.java:8: error: [Dagger/MissingBinding] sn0w.test.crypto.chart.LatestChart cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
public abstract interface AppComponent {
^
A binding with matching key exists in component: sn0w.test.crypto.di.AppComponent
sn0w.test.crypto.chart.LatestChart is injected at
sn0w.test.crypto.activities.ChartActivity.latestChart
sn0w.test.crypto.activities.ChartActivity is injected at
sn0w.test.crypto.di.AppComponent.inject(sn0w.test.crypto.activities.ChartActivity)
您当前尝试将依赖项注入 LatestChart
的方式是如何满足 Dagger 不会创建的对象(例如活动)中的依赖项。如果您自己创建 LatestChart
对象(只是 val latestCharts = LatestCharts()
),您会看到 context
和 formatter
实际上被注入到这个新创建的实例中。
然而,如果你想让 Dagger 将一个 LatestChart
对象注入另一个对象(根据编译错误我假设是 ChartActivity
),你必须让 Dagger 知道如何创建它的实例。有两种方法可以实现:
1.用 @Inject
LatestChart
构造函数
class LatestChart @Inject constructor(
private val context: Context,
private val formatter: YearValueFormatter
) {
lateinit var chart: LineChart
fun initChart(chart: LineChart) { ... }
fun addEntry(value: Float, date: Float) { ... }
private fun createSet(): LineDataSet { ... }
}
2。在 Dagger 模块之一中创建提供程序方法
class LatestChart(private val context: Context, private val formatter: YearValueFormatter) {
lateinit var chart: LineChart
fun initChart(chart: LineChart) { ... }
fun addEntry(value: Float, date: Float) { ... }
private fun createSet(): LineDataSet { ... }
}
@Module
class LatestChartModule {
@Module
companion object {
@JvmStatic
@Provides
fun provideLatestChart(context: Context, formatter: YearValueFormatter): LatestChart =
LatestChart(context, formatter)
}
}