lateinit 属性 ABC 尚未初始化

lateinit property ABC has not been initialized

点击我在recyclerView中实现的属于activity_main.xml(MainActivityclass)的按钮后,工作是切换activity(MainActivity -> EmployeeActivity),出现如下问题。我将不胜感激。

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ing, PID: 31027
    kotlin.UninitializedPropertyAccessException: lateinit property context has not been initialized
        at com.example.ing.adapter.MyAdapter.getContext(MyAdapter.kt:20)
        at com.example.ing.adapter.MyAdapter$onBindViewHolder.onClick(MyAdapter.kt:37)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

MyAdapter class:

class MyAdapter :RecyclerView.Adapter<MyAdapter.MyViewHolder>(){

    lateinit var context:Context

    private var myList = emptyList<Data>()
    inner class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_employee_item, parent, false))
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemView.tvEmployeeName.text = myList[position].employeeName
        holder.itemView.showButton.setOnClickListener{
            val intent = Intent(context.applicationContext,EmployeeActivity::class.java)
            intent.putExtra("id", position)
            context.startActivity(intent)

        }
    }

    fun setData(newList:List<Data>){
        myList = newList
        notifyDataSetChanged()
    }
}

您声明了 lateinit var context:Context 变量,但您尚未为其分配任何上下文值。应该先赋值再使用。