Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP + Dagger2 + Kotlin: lateinit property presenter has not been initialized

I setup my project to MVP architecture. I was able to setup MVP for MainActivity and ListAdapter. Basically I have a list of images in ListAdapter, when item clicked, it will start PhotoDetailActivity which I've setup Presenter and View already.

ListAdapter.kt (Full gist 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 (Full gist 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)
}

But it said that:

kotlin.UninitializedPropertyAccessException: lateinit property presenter has not been initialized

My full code is here

like image 768
truongnm Avatar asked Oct 21 '25 15:10

truongnm


1 Answers

declaring the member like this:

lateinit var presenter: PhotoDetailContract.Presenter

..inidicates that this reference will be initialized at some point after the constructor has run. however, you don't seem to have any code instrumented to do so. having glanced at your project, i think what you might have meant was for Dagger to inject that that instance. to do so, you need to add the @Inject annotation, like so:

@Inject lateinit var presenter: PhotoDetailContract.Presenter
like image 56
homerman Avatar answered Oct 23 '25 08:10

homerman