Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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()
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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() 
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading