【问题标题】:How to fix "[Dagger/MissingBinding]" in kotlin?如何在 kotlin 中修复“[Dagger/MissingBinding]”?
【发布时间】:2019-08-01 08:00:13
【问题描述】:

我正在尝试在 AppComponent 中注入 LatestChart 并解决 [Dagger/MissingBinding] 如果没有 @Inject 构造函数或 @Provides-annotated 方法就无法提供最新图表... 提前感谢您的帮助

最新图表.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)



【问题讨论】:

    标签: kotlin dagger


    【解决方案1】:

    您当前尝试将依赖项注入LatestChart 的方式是满足 Dagger 不会创建的对象(例如活动)中的依赖项。如果您自己创建LatestChart 对象(只是val latestCharts = LatestCharts()),您会看到contextformatter 实际上被注入到这个新创建的实例中。

    但是,如果您希望 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多