android-studio - 错误:未解决的参考: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)
}
弹出的错误在上下文中。当我将鼠标悬停在它上面时,错误显示“未解决的参考:上下文”。我不知道如何解决它,谷歌似乎没有任何问题出现。显示上下文操作似乎没有帮助,因为它什么都不做,红色的上下文仍然存在。
解决方案
将类声明更改为:
class DashboardAdapter(val context: Context, ...
推荐阅读
- python-3.x - 使用 unicode 打印 sympy 符号
- angular - Azure 认知服务 - TTS - SSML 编辑器
- reactjs - 在样式化组件上传播输入道具会导致打字稿错误
- mysql - 使用 Insert Ignore 时插入其他值
- nuxt.js - 如何将 Bootstrap5 的 Masonry 正确加载到 Nuxt?
- javascript - 使用 data-id ajax 从服务器中删除带有图像的 div
- spring-boot - Spring boot IoC:相同的bean定义,不同的分辨率
- django - Django中排序的过滤查询集中的下一个或上一个项目
- symfony - 使用 xampp 创建 symfony 数据库
- r - R中数据框列和2个列表之间的文本挖掘