Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class called medicine, I got an error when writing adapter = MedicineAdapter(this, this.medicineList!!) in main activity I saw the error in run window. My app is crashing when I want to run and I saw one error which is about adapter's line

Also, I have medicine_items it is my custom design I assigned it to the adapter in my adapter also I checked my ids but ? don t know why am I got an error

MainActivity

class MainActivity : AppCompatActivity() {


private var adapter: MedicineAdapter? = null
private var medicineList : ArrayList<Medicine>? = null
private var recyclerView: RecyclerView? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    recyclerView =
            findViewById<View>(R.id.recycler) as RecyclerView

    adapter = MedicineAdapter(this, this.medicineList!!)
    val layoutManager = LinearLayoutManager(applicationContext)

    recyclerView!!.layoutManager = layoutManager
    recyclerView!!.itemAnimator = DefaultItemAnimator()

    // Add a neat dividing line between items in the list
    recyclerView!!.addItemDecoration(
            DividerItemDecoration(this,
                    LinearLayoutManager.VERTICAL))

    // set the adapter
    recyclerView!!.adapter = adapter
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
    R.id.addBtn -> {

        val intent = Intent(this,AddNewMedicine::class.java)
        startActivity(intent)


        true
    }
    else -> super.onOptionsItemSelected(item)
}


fun addMedicine(m: Medicine){

    medicineList!!.add(m)
    adapter!!.notifyDataSetChanged()

}

}

MyAdapter

class MedicineAdapter(

    private val mainActivity: MainActivity,
    private val medicineList: ArrayList<Medicine>)

: RecyclerView.Adapter<MedicineAdapter.ListItemHolder>(){


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

        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.medicine_items, parent, false)

        return ListItemHolder(itemView)

    }
    inner class ListItemHolder(view: View) :
            RecyclerView.ViewHolder(view),
            View.OnClickListener {

        internal var name = view.findViewById<TextView>(R.id.name)

        internal var amount = view.findViewById<TextView>(R.id.amount)

        internal var description = view.findViewById<TextView>(R.id.description)


        init {
            view.isClickable = true
            view.setOnClickListener(this)

        }


        override fun onClick(view: View) {

            //val intentToCarPager = Intent(view!!.context, CarPagerActivity::class.java)

            //view.context.startActivity(intentToCarPager)

        }




    }

    override fun onBindViewHolder(holder: ListItemHolder, position: Int) {

        val medicine = medicineList!![position]

        holder.name.text = medicine.name.toString()

        holder.amount.text = medicine.amount.toString()

        holder.description.text = medicine.desription.toString()



    }

    override fun getItemCount(): Int {
        if (medicineList != null) {
            return medicineList.size
        }
        return -1
    }

}

my run window

question from:https://stackoverflow.com/questions/65646119/kotlin-adapter-give-me-error-in-mainactivity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's because here:

private var medicineList : ArrayList<Medicine>? = null

you have initialized medicineList as null and you haven't given it a proper value along the way and here:

adapter = MedicineAdapter(this, this.medicineList!!)

you have asserted that it is not null and tried to assign it to the adapter.

If you have no elements to add to your array at first, initialize it this way:

private var medicineList : ArrayList<Medicine> = arrayListOf()

and then you can add elements to it:

medicineList.add(myMedicine)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...