首页 > 解决方案 > 错误:未解决的参考:LayoutInflater 中的上下文(上下文)

问题描述

我是 kotlin 和 android studio 开发的新手。我正在尝试制作一个简单的待办事项列表应用程序,它有一个“newTask”按钮来添加新任务。在主布局 kotlin 文件中,我为按钮添加了以下命令:

 newTask.setOnClickListener{
            val dialog = AlertDialog.Builder(this,)
            val view = layoutInflater.inflate(R.layout.dialogue_dashboard, null)
            val toDoName = view.findViewById<EditText>(R.id.et_todo)
            dialog.setView(view)            
            dialog.setPositiveButton("Add"){ _: DialogInterface, _ : Int ->
                if(toDoName.text.isNotEmpty()){
                    val toDo = ToDo()
                    toDo.name = toDoName.text.toString()
                    dbHandller.addToDo(toDo)
                }
            }
            dialog.setNegativeButton("Cancel"){ _: DialogInterface, _: Int ->

            }
            dialog.show()
        }

我也在使用回收器视图来使其全部工作,并且我正在为回收器视图创建适配器:

class DashboardAdapter(context: Context,val list: MutableList<ToDo>) : RecyclerView.Adapter<DashboardAdapter.ViewHolder>(){
        class ViewHolder(v : View) : RecyclerView.ViewHolder(v){
            val toDoName : TextView = v.findViewById(R.id.tv_todo_name)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_child_layout)

        }

弹出的错误在上下文中。当我将鼠标悬停在它上面时,错误显示“未解决的参考:上下文”。我不知道如何解决它,谷歌似乎没有任何问题出现。显示上下文操作似乎没有帮助,因为它什么都不做,红色的上下文仍然存在。

标签: android-studiokotlinandroid-recyclerview

解决方案


将类声明更改为:

class DashboardAdapter(val context: Context, ...

推荐阅读