Home > Enterprise >  Unable to add window -- token null is not valid; is your activity running error. How can I solve it?
Unable to add window -- token null is not valid; is your activity running error. How can I solve it?

Time:03-14

I have a login screen. Switching to the HomeScreenActivity when the user logs in. I want to show a popup window message about user information when the activity is opened. But I'm getting an 'Unable to add window -- token null is not valid; is your activity running?' error. I have a popup layout that name is popup_user_information. I'm using view binding for this project. How can I solve it?

package com.tcoding.instagramloginscreen

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Display
import android.view.Gravity
import android.widget.PopupWindow
import android.widget.Toast
import androidx.core.hardware.display.DisplayManagerCompat
import androidx.viewpager.widget.ViewPager
import com.tcoding.instagramloginscreen.databinding.ActivityHomePageBinding
import com.tcoding.instagramloginscreen.databinding.PopupUserInformationBinding

class HomePageActivity : AppCompatActivity() {

    lateinit var binding: ActivityHomePageBinding
    lateinit var bindingPopup: PopupUserInformationBinding


    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHomePageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val username = intent.getStringExtra("username")
        val password = intent.getStringExtra("password")
        binding.tvUsername.text = binding.tvUsername.text.toString()   username
        binding.tvPassword.text = binding.tvPassword.text.toString()   password

        binding.btnExit.setOnClickListener{
            val intent = Intent(this, MainActivity::class.java)
            intent.putExtra("username", username)
            intent.putExtra("password", password)
            setResult(RESULT_OK)
            finish()

        }

        popUp()


    }


    fun popUp()
    {
        val username = intent.getStringExtra("username")
        val password = intent.getStringExtra("password")
        bindingPopup = PopupUserInformationBinding.inflate(layoutInflater)
        val popup = PopupWindow(bindingPopup.root, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT)


        popup.showAtLocation(bindingPopup.root, Gravity.BOTTOM, 0,0)

        bindingPopup.usernameInfo.text = username
        bindingPopup.passwordInfo.setText(password)

        bindingPopup.btnKaydet.setOnClickListener {
            Toast.makeText(this,"Clicked save",Toast.LENGTH_SHORT).show()
            popup.dismiss()
        }
        bindingPopup.btnNotnow.setOnClickListener {
            Toast.makeText(this,"Clicked Not now",Toast.LENGTH_SHORT).show()
            popup.dismiss()
        }
    }
}

CodePudding user response:

You're trying to show the popup too early. The view on OnCreate is still not created so try to move the popup() to OnViewCreated()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    popup()
}

or use View.post so it goes into the main thread queue and gets executed after the other pending tasks are finished

override fun onCreate(savedInstanceState: Bundle?) {
   ...
   binding.root.post {
     popup() 
   }
}
  • Related