java - 错误 : android. view.WindowManager$BadTokenException : Unable to add window -- token null is not valid; is your activity running?

标签 java android kotlin android-alertdialog android-windowmanager

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

您传递给 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/

相关文章:

java - Parcelable 类中的 Parcelable 列表未使用 Parcel.readTypedList 回读

android - Worklight 音频播放和控制

kotlin - Kotlin JVM 和 Kotlin Native 有什么区别?

types - Kotlin 具体化的类型参数不能用作函数体中的类型参数

android - 传输事件的 FileObserver

ios - 构建 iOS 框架时找不到 Ktor 和 kotlinx 依赖项

java - 使用 AspectJ 捕获参数自定义注释

java - foo.setText(bar) 导致 NullPointerException..我做错了什么?

java - 使用java中的 volatile boolean 标志从 HashMap 中的另一个线程停止一个线程

android - 使用 Canvas (surfaceview) 或使用带有平移动画的 View