MVP + Dagger2 + Kotlin:lateinit属性演示者尚未初始化

时间:2018-07-30 02:12:30

标签: kotlin dagger-2 mvp onitemclicklistener

我将项目设置为MVP架构。我能够为MainActivityListAdapter设置MVP。基本上,我在ListAdapter中有一个图像列表,当单击项目时,它将启动PhotoDetailActivity,我已经设置了PresenterView

ListAdapter.kt(完整摘要here

override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
    val photo = photos[position]
    holder.bind(photo)
    holder.itemLayout.setOnClickListener {
        listener.itemDetail(photo.id)
    }
    Glide
            .with(context)
            .load(photo.urls.regular)
            .into(holder.photo)
    Glide
            .with(context)
            .load(photo.user.profile_image.small)
            .into(holder.userAvatar)
}

PhotoDetailActivity.kt(完整摘要here

lateinit var presenter: PhotoDetailContract.Presenter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_photo_detail)
    var photoId: String = intent.getStringExtra("photoId")

    injectDependency()
    presenter.attach(this)
    initView(photoId)
}

但是它说:

kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized

我的完整代码是here

1 个答案:

答案 0 :(得分:2)

这样声明成员:

lateinit var presenter: PhotoDetailContract.Presenter

..表明该引用将在构造函数运行后的某个时刻初始化。但是,您似乎没有任何代码可以执行此操作。看完您的项目后,我认为您可能想让Dagger注入该实例。为此,您需要添加@Inject批注,如下所示:

@Inject lateinit var presenter: PhotoDetailContract.Presenter