Home > OS >  lateinit property mClickListener has not been initialized Adapter kotlin
lateinit property mClickListener has not been initialized Adapter kotlin

Time:07-05

i make adaper onclick listener but somehow theres error message lateinit property mClickListener has not been initialized Adapter kotlin

variable on click

lateinit var mClickListener: ItemClickListener

base view holder

inner class AdapterListProjectVH2(itemView: View) : BaseViewHolder(itemView),
        View.OnClickListener {
        private val cvItemViewVisualInsp: CardView =
            itemView.findViewById(R.id.cv_itemview_visual_insp)
        private val imgMarkOnMap: ImageView = itemView.findViewById(R.id.img_mark_on_map)
        private val imgStatusSync: ImageView = itemView.findViewById(R.id.img_status_sync)
        //private val imgStatusRegister : ImageView = itemView.findViewById(R.id.img_status_register)

        private val tvFootpathTypeName: TextView = itemView.findViewById(R.id.tv_foot_type_name)
        private val tvSeverityTypeName: TextView = itemView.findViewById(R.id.tv_severity_type)
        private val tvRepairMethodName: TextView = itemView.findViewById(R.id.tv_repair_method_name)
        private val tvRfCreatedDate: TextView =
            itemView.findViewById(R.id.tv_rf_created_date)
        private val clInspected: ConstraintLayout = itemView.findViewById(R.id.cl_inspected)

        private val imageSlider: SliderView = itemView.findViewById(R.id.imageSlider)

        init {
            cvItemViewVisualInsp.setOnClickListener(this)
        }

overide click function

override fun onClick(v: View?) {
            mClickListener.onClickItem(adapterPosition, itemView, mData?.get(adapterPosition))
        }
 fun setOnItemClickListener(clickListener: ItemClickListener) {
        mClickListener = clickListener
    }

    fun clear() {
        this.mData!!.clear()
        notifyDataSetChanged()
    }

    interface ItemClickListener {
        fun onClickItem(
            pos: Int,
            aView: View,
            data: ViewFootpathEntityWIthAllData?
        )
    }

Activity call onclick listerner

  footPathadapter = FootpathAdapter(this, footPathDataList)
        footPathadapter?.setOnItemClickListener(object : FootpathAdapter.ItemClickListener{
            override fun onClickItem(pos: Int, aView: View, data: ViewFootpathEntityWIthAllData?) {
                startActivity(Intent(this@FormFootpathActivity, MainActivity::class.java))
            }
        })

CodePudding user response:

You are creating the Adapter instance before passing mClickListener, when you create the adapter an instance of AdapterListProjectVH2 is going to be created and the this code is going to be executed:

    init {
        cvItemViewVisualInsp.setOnClickListener(this)
    }

Which will cose the error because this cause is going to be executed before you set the mClickListener.

So you can initialize mClickListener as null like that:

var mClickListener: ItemClickListener? = null

And add a null check when call it like that:

override fun onClick(v: View?) {
    mClickListener?.onClickItem(adapterPosition, itemView, mData?.get(adapterPosition))
}

CodePudding user response:

I solve by my self, it because i put the code in wrong activity

  • Related