0
我正在嘗試使用Dagger 2創建簡單的MVP Archtecture應用程序。我試圖獲得與本教程中相同的結果,但使用Kotlin。這是我的代碼到目前爲止。Dagger 2 + Kotlin無法將Presenter插入視圖
主講人:
class MainPresenter @Inject constructor(var view: IMainView): IMainPresenter{
override fun beginMessuring() {
view.toastMessage("Measuring started")
}
override fun stopMessuring() {
view.toastMessage("Measuring stopped")
}
}
檢視:
class MainActivity : AppCompatActivity(), IMainView {
@Inject lateinit var presenter : MainPresenter
val component: IMainComponent by lazy {
DaggerIMainComponent
.builder()
.mainPresenterModule(MainPresenterModule(this))
.build()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
component.inject(this)
presenter.beginMessuring()
}
override fun toastMessage(message: String) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
}
匕首模塊:
匕首組件:
@Component(modules = arrayOf(MainPresenterModule::class))
interface IMainComponent {
fun inject(mainView : IMainActivity)
}
的問題是,我正在建立自己的錯誤,其始於此:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maciej.spiritlvl/com.example.maciej.spiritlvl.View.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized
PS,我匕首的gradle配置:
kapt 'com.google.dagger:dagger-compiler:2.9'
mplementation 'com.google.dagger:dagger:2.9'
編輯: 改變注入主持人從IMainView到的MainView類型。
我做了你如何提到。在視圖中,我將更改的字段類型替換爲MainPresenter,但仍然收到kotlin.UninitailizedPropertyException。 – Maciek