skip to Main Content

I have faced the problem of my application always show keep stopping. It show me the problem is not been initialized. I have try insert findViewbyId for mListener but it say i cannot insert id in constraint layout so mean this way cannot be used. Can somebody help me? Thank for your help

Below are my original code which problem out :

package com.example.assignment_mad

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.imageview.ShapeableImageView

class NotificationAdapter(private val companysList:ArrayList<Company>):RecyclerView.Adapter<NotificationAdapter.MyViewHolder>() {

    private lateinit var mListener:onItemClickListener

    interface onItemClickListener{
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: onItemClickListener){
        mListener=listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val itemView=LayoutInflater.from(parent.context).inflate(R.layout.list_item,parent,false)
        return MyViewHolder(itemView,mListener)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem=companysList[position]
        holder.titleImage.setImageResource(currentItem.titleImage)
        holder.tvHeading.text=currentItem.heading
    }

    override fun getItemCount(): Int {
        return companysList.size
    }

    //to insert the post detail
    class MyViewHolder(itemView: View,listener: onItemClickListener):RecyclerView.ViewHolder(itemView){

        val titleImage:ShapeableImageView=itemView.findViewById(R.id.title_image)
        val tvHeading: TextView =itemView.findViewById(R.id.tvHeading)

        init {
            itemView.setOnClickListener{
                listener.onItemClick(adapterPosition)
            }
        }
    }

}

The list_item xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="8dp">

    <com.google.android.material.imageview.ShapeableImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/title_image"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:shapeAppearanceOverlay="@style/RoundCorner"
        android:src="@drawable/company_logo_1"/>

    <TextView
        android:id="@+id/tvHeading"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:text="Candidate Biden Called Saudi Arable a Pareft eaft."
        android:textSize="16dp"
        android:textStyle="bold"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/title_image"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_image"
        android:background="@color/underline"/>


</androidx.constraintlayout.widget.ConstraintLayout>

The problem showing :

    kotlin.UninitializedPropertyAccessException: lateinit property mListener has not been initialized
        at com.example.assignment_mad.NotificationAdapter.onCreateViewHolder(Notificatiion_Adapter.kt:25)
        at com.example.assignment_mad.NotificationAdapter.onCreateViewHolder(Notificatiion_Adapter.kt:10)

2

Answers


  1. Add the Nullable check for onItemClickListener type of variable will solve your problem.

    package com.example.assignment_mad
    
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.TextView
    import androidx.recyclerview.widget.RecyclerView
    import com.google.android.material.imageview.ShapeableImageView
    
    class NotificationAdapter(private val companysList:ArrayList<Company>):RecyclerView.Adapter<NotificationAdapter.MyViewHolder>() {
    
        private var mListener:onItemClickListener?=null
    
        interface onItemClickListener{
            fun onItemClick(position: Int)
        }
    
        fun setOnItemClickListener(listener: onItemClickListener){
            mListener=listener
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    
            val itemView=LayoutInflater.from(parent.context).inflate(R.layout.list_item,parent,false)
            return MyViewHolder(itemView,mListener)
        }
    
        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            val currentItem=companysList[position]
            holder.titleImage.setImageResource(currentItem.titleImage)
            holder.tvHeading.text=currentItem.heading
        }
    
        override fun getItemCount(): Int {
            return companysList.size
        }
    
        //to insert the post detail
        class MyViewHolder(itemView: View,listener: onItemClickListener?):RecyclerView.ViewHolder(itemView){
    
            val titleImage:ShapeableImageView=itemView.findViewById(R.id.title_image)
            val tvHeading: TextView =itemView.findViewById(R.id.tvHeading)
    
            init {
                itemView.setOnClickListener{
                    listener?.onItemClick(adapterPosition)
                }
            }
        }
    
    }
    
    Login or Signup to reply.
  2. Your problem is you are not initilizing the mListener properly

    kotlin.UninitializedPropertyAccessException: lateinit property mListener has not been initialized at com.example.assignment_mad.NotificationAdapter.onCreateViewHolder(Notificatiion_Adapter.kt:25) at com.example.assignment_mad.NotificationAdapter.onCreateViewHolder(Notificatiion_Adapter.kt:10)

    My solution is to pass the listener in the adapter constructor

    class NotificationAdapter(
        private val companysList:ArrayList<Company>,
        private val listener:onItemClickListener
    ){
    
     // delete 
     private var mListener:onItemClickListener?=null
     // and
     fun setOnItemClickListener(listener: onItemClickListener){
            mListener=listener
        }
    
    }
    

    use it in activity/fragment

    
    adapter = NotificationAdapter(list, object: NotificationAdapter.onItemClickListener{
        override fun onItemClick(position:Int){
    
       }
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search