我正在学习如何使用Dagger 2
和MVP
体系结构。
但是我现在陷入了这个错误:
未解决的参考:DaggerHelloComponent
看,这是我的模块:
@Module
class HelloModule {
lateinit var activityDagger: HelloActivityDagger
constructor(activityDagger: HelloActivityDagger) {
this.activityDagger = activityDagger
}
@Provides
fun providesHelloPresenter(): HelloActivityPresenterDagger = HelloActivityPresenterDagger(activityDagger)
}
和我的组件:
@Component(modules = [HelloModule::class])
interface HelloComponent {
fun inject(activityDagger: HelloActivityDagger)
}
因此,当我尝试在DaggerHelloComponent.create().inject(this)
中构建像HelloActivityDagger
这样的组件时,会向我显示上面的错误。
有人知道我在做什么错吗?
因为我可以看到这段代码有什么问题。
哦,我已经按照问题Unresolved reference DaggerApplicationComponent的问题kapt
进行了跟踪,什么都没发生
编辑
为了更容易理解,我已经将项目上传到Git。 https://github.com/luangs7/DaggerMvpExample
答案 0 :(得分:0)
将此代码放在MyApplication
类中对我有用,以从您的GitHub项目开始创建模块-在第一个失败的构建之后,kapt
生成DaggerHelloComponent
类,我可以导入它。
import android.app.Application
import br.com.squarebits.mvpexample.DaggerMvp.componentes.DaggerHelloComponent
import br.com.squarebits.mvpexample.DaggerMvp.componentes.HelloComponent
import br.com.squarebits.mvpexample.DaggerMvp.modules.HelloModule
class MyApplication : Application() {
val component: HelloComponent by lazy {
DaggerHelloComponent.builder()
.helloModule(HelloModule(HelloActivityDagger()))
.build()
}
}
如果这完全没有发生,则应尝试使用File -> Invalidate Caches / Restart
重新启动Studio,清理和重建项目等常规调试步骤。也许尝试将GitHub存储库拉到新位置,看看是否可以为您量身打造-代码不是问题。