错误:android.view.WindowManager $ BadTokenException:无法添加窗口-令牌null无效;您的活动正在运行吗?

问题描述 投票:0回答:1

我是一名学习者,正在尝试以另一种单独创建的布局显示一些设置数据。我知道这可以通过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()

        }



    }

我也尝试过一些选项,例如在清单文件中设置权限,但错误仍然存​​在

java android kotlin android-alertdialog android-windowmanager
1个回答
0
投票

[Activity,需要Context作为Dialog

您传递给context构造函数的Dialog可能不是活动。

© www.soinside.com 2019 - 2024. All rights reserved.