【问题标题】:Unresolved reference to DaggerComponent未解决的对 DaggerComponent 的引用
【发布时间】:2019-11-06 12:33:44
【问题描述】:

下面提供的代码仍然具有对 DaggerAppComponent 的未解析引用。我找不到错误。

已经重建

这是我的应用程序类

class CinemaApplication : SplitCompatApplication() {

    private lateinit var context: Context

    val appComponent: AppComponent by lazy {
        DaggerAppComponent.factory().create(applicationContext)
    }
}

这是我的 AppComponent 类

@Component
interface AppComponent {

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance context: Context): AppComponent
    }

    fun inject(activity: NavHostActivity)
}

这是我的活动课

class NavHostActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        (application as CinemaApplication).appComponent.inject(this)

        super.onCreate(savedInstanceState)
    }

【问题讨论】:

  • 在 DaggerAppComponent.factory().create(applicationContext) 行中,您是否尝试将“applicationContext 更改为 MainActivity.class(例如)或使用 getapplicationcontext()
  • @RubenMeiring getapplicationcontext() = Kotlin 世界中的 applicationcontext。
  • stackoverflow.com/questions/42843996/… 也许这里的东西可以帮助你

标签: android dagger-2 dagger


【解决方案1】:

尝试使用kapt

apply plugin: 'kotlin-kapt'

dependencies {
    implementation "com.google.dagger:dagger:2.22"
    kapt "com.google.dagger:dagger-compiler:2.22"
}

【讨论】:

  • 请同时添加您的 gradle 文件
  • 也可以在 AS 中使用invalidate cache and restart
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 2012-04-25
相关资源
最近更新 更多