Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

android studio - kotlin adapter give me error in mainactivity

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...