WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
A dialog or popup was shown with a context whose Activity is finishing/destroyed, or with the Application context.
Seen on:
Android
Meaning
Showing dialogs after async work completes, when the user already left the screen, or using applicationContext for dialogs crashes. Check lifecycle state or use DialogFragment.
Common causes
- Showing a dialog after the Activity finished
- Using Application context for a Dialog/PopupWindow
- Showing UI from a background service
⚡ Quick fix
- Check !isFinishing && !isDestroyed before showing
- Use the Activity context or DialogFragment
- Tie async callbacks to lifecycle (lifecycleScope)
Detailed fix by platform
Kotlin
if (!isFinishing && !isDestroyed) dialog.show()
How to diagnose
- Context — Activity or Application?
- Timing — After async work?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- ActivityNotFoundException ActivityNotFoundException: Unable to find explicit activity class; have you declared this activity in your AndroidManifest.xml?
- An unhandled error has occurred Blazor: An unhandled error has occurred. Reload 🗙
- Android resource linking failed AAPT: error: resource x (aka y) not found — Android resource linking failed
Most viewed in Android
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026