Android studio kotlin适配器在mainactivity中给我错误

Android studio kotlin适配器在mainactivity中给我错误,android-studio,kotlin,Android Studio,Kotlin,我有一个名为medicine的类,在主活动中编写adapter=MedicineAdapter(this,this.medicineList!!)时出错,我在运行窗口中看到了错误。 当我想运行时,我的应用程序崩溃了,我看到了一个关于适配器线路的错误 还有,我有一些药品,这是我的定制设计,我把它分配给了适配器中的适配器,我也检查了我的ID,但我不知道为什么会出错 main活动 class MainActivity : AppCompatActivity() { private var adap

我有一个名为medicine的类,在主活动中编写adapter=MedicineAdapter(this,this.medicineList!!)时出错,我在运行窗口中看到了错误。 当我想运行时,我的应用程序崩溃了,我看到了一个关于适配器线路的错误

还有,我有一些药品,这是我的定制设计,我把它分配给了适配器中的适配器,我也检查了我的ID,但我不知道为什么会出错

main活动

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()

}
class MainActivity:AppCompatActivity(){
专用变量适配器:MedicineAdapter?=null
私有var medicineList:ArrayList?=null
私有变量recyclerView:recyclerView?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
回收视图=
findViewById(R.id.recycler)作为RecyclerView
adapter=MedicineAdapter(this,this.medicineList!!)
val layoutManager=LinearLayoutManager(applicationContext)
回收视图!!.layoutManager=layoutManager
recyclerView!!.itemAnimator=DefaultItemAnimator()
//在列表中的项目之间添加一条整齐的分隔线
回收视图!!。添加装饰(
DividerItemDecoration(本,
直线布局管理器(垂直)
//设置适配器
recyclerView!!.adapter=适配器
}
重写创建选项菜单(菜单:菜单):布尔值{
菜单充气器(右菜单菜单,菜单)
返回真值
}
覆盖选项ItemSelected(项:MenuItem):布尔=何时(项.项ID){
R.id.addBtn->{
val intent=intent(这是AddNewMedicine::class.java)
星触觉(意图)
真的
}
else->super.onOptionsItemSelected(项目)
}
医学(m:医学){
药物列表!!.add(m)
适配器!!.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
    }
class MedicineAdapter(
私有val mainActivity:mainActivity,
私人val药物列表:ArrayList)
:RecyclerView.Adapter(){
覆盖视图保持器(
父级:ViewGroup,viewType:Int):ListItemHolder{
val itemView=LayoutInflater.from(parent.context)。充气(R.layout.medicine_items,parent,false)
返回ListItemHolder(itemView)
}
内部类ListItemHolder(视图:视图):
RecyclerView.ViewHolder(视图),
View.OnClickListener{
内部变量名称=view.findviewbyd(R.id.name)
内部var金额=view.findViewById(R.id.amount)
内部变量描述=view.findViewById(R.id.description)
初始化{
view.isClickable=true
view.setOnClickListener(此)
}
覆盖趣味onClick(视图:视图){
//val intentotcarpager=Intent(视图!!.context,CarPagerActivity::class.java)
//view.context.startActivity(intentToCarPager)
}
}
覆盖BindViewHolder(holder:ListItemHolder,位置:Int){
val medicine=药物列表!![位置]
holder.name.text=medicine.name.toString()
holder.amount.text=medicine.amount.toString()
holder.description.text=medicine.description.toString()
}
重写getItemCount():Int{
if(medicineList!=null){
返回medicineList.size
}
返回-1
}
}

这是因为:

private var medicineList : ArrayList<Medicine>? = null
您已声明它不是null,并尝试将其分配给适配器

如果一开始没有要添加到数组中的元素,请按以下方式对其进行初始化:

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

你能提供你的错误stacktrace吗?@mhdwajeeh.95我已经添加了一个图像,你想要这个吗?我是kotlin Sorry的新手,请以文本而非图像的形式添加碰撞报告。在主活动的第29行有什么内容?我相信您正在访问一个空对象。@MehranB Stackoverflow说我添加了太多的代码,所以我无法访问,问题是我一开始有一个空的药物列表。在我的主要活动中,我在右侧的操作栏中有一个回收器视图和一个加号按钮。如果主要活动出现,我将单击我的按钮并切换到另一个事件。我将向我的数组中添加一些数据。我看到了。所以上面的答案应该是可行的。您还有其他问题吗?当我将null更改为arrayListof()时,主活动现在不会崩溃。谢谢!最后,我如何在main中调用我的函数,在这里我添加数据,它的名称是addMedicine,在这里我添加对象也是一个活动,因此我如何在main中调用我的方法首先,当您有其他问题时,您不需要取消标记正确的答案。如果您有更多问题,请分别提问。Stackoverflow不是这样工作的。我知道你是这个网站的新手,但这并不酷。第二,你的第二个问题取决于你的应用程序的架构,需要更多的澄清才能回答。因此,提出一个新的问题。
private var medicineList : ArrayList<Medicine> = arrayListOf()
medicineList.add(myMedicine)