我是一名学习者,正在尝试在另一个单独创建的布局中显示一些设置数据。我知道这可以通过 Intent 来完成,但我一直在尝试根据教程视频使用 Dialog。
下面是代码
override fun onBindViewHolder(holder:ContactViewHolder, position:Int){
holder.name.text = data[position].name
holder.phoneNumber.text = data[position].phone
holder.image.setImageResource(data[position].image)
holder.card.setOnClickListener {
Toast.makeText(context, data[position].phone, Toast.LENGTH_LONG).show()
val profilePage = Dialog(context)
profilePage.setContentView(R.layout.profile)
profilePage.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
profilePage.setTitle("Profile page")
val profileName = profilePage.findViewById<TextView>(R.id.profileName)
val profileEmail = profilePage.findViewById<TextView>(R.id.profileEmail)
val profileImage = profilePage.findViewById<ImageView>(R.id.profileImage)
profileName.text = data[position].name
profileEmail.text = data[position].phone
profileImage.setImageResource(data[position].image)
profilePage.show()
}
}
我还尝试了多种选项,例如在 list 文件中设置权限,但错误仍然存在
最佳答案
您需要一个Activity
作为Dialog
的Context
。
您传递给 Dialog
构造函数的 context
可能不是 Activity 。
关于java - 错误 : android. view.WindowManager$BadTokenException : Unable to add window -- token null is not valid; is your activity running?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58781447/