대거 2를 사용하여 간단한 MVP Archtecture 응용 프로그램을 만들려고합니다.이 튜토리얼에서와 같은 결과를 얻으려고하지만 Kotlin을 사용하고 있습니다. 지금까지 제 코드가 있습니다.대거 2 + Kotlin이보기에 발표자를 삽입 할 수 없습니다.
발표자 :
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()
}
}
대거 모듈 :
@Module
class MainPresenterModule(private val view: IMainView) {
@Provides
fun provideView() = view
}
단검 구성 요소 :
@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'
편집 : MAINVIEW에 IMainView에서 변경 주입 발표자 유형을
문제는 내가이 시작 오류를 구축 얻고 있다는 것입니다.
나는 당신이 언급 한 방법을했다. 보기에서 변경된 필드 유형을 MainPresenter로 바꿨지 만 여전히 kotlin.UninitailizedPropertyException이 발생합니다. – Maciek