android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
See original GitHub issuePlease complete the following information:
- Library Version [v1.1.9]
- Affected Device(s) [General]
Describe the Bug:
We are receiving this crash in our crashlytics console
Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:710)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1380)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1236)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1193)
at com.skydoves.balloon.Balloon$showAlignTop$$inlined$show$1.run(Balloon.java:1272)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
This is a dummy implementation of our view for a reference implementation:
class MyCustomView(context: Context) : FrameLayout(context), LifecycleOwner {
private val myLifeCycle: LifecycleRegistry = LifecycleRegistry(this)
private val binding: MyCustomViewBinding by lazy {
MyCustomViewBinding.inflate(
LayoutInflater.from(context),
this,
true
)
}
private val myBalloon by lazy {
createBalloon(context) {
setCornerRadius(4f)
setPadding(14)
setBackgroundColor(ContextCompat.getColor(context, R.color.text_black))
setWidthRatio(1f)
setTextResource(R.string.if_you_want_to_take_more_orders_turn_on_the_auto_acceptance)
setTextColorResource(R.color.white)
setTextSize(14f)
setIconDrawable(context.getDrawableCompat(R.drawable.icon_close))
setIconSize(16)
dismissWhenClicked = true
lifecycleOwner = this@MyCustomView
}
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
myLifeCycle.handleLifecycleEvent(Lifecycle.Event.ON_START)
}
override fun onDetachedFromWindow() {
myLifeCycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
super.onDetachedFromWindow()
}
fun observe(viewModel: ViewModel) {
viewModel.someLiveData.observe(this@MyCustomView) {
myBalloon.showAlignTop(binding.someView)
}
}
}
Expected Behavior:
Don’t receive these crashes.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Unable to add window -- token null is not valid - Stack Overflow
WindowManager$BadTokenException : Unable to add window -- token null is not valid; is your activity running? but I have no activity? I want...
Read more >Unable to add window -- token null is not for an application
android. view. WindowManager$BadTokenException : Unable to add window -- token null is not for an application at android.view.ViewRootImpl.
Read more >Unable to add window -- token null is not valid is your activity ...
I am trying to put a feature of a custom popup menu that shows up when the user clicks on the floating icons....
Read more >Android: annoying exception Unable to add window – is your ...
WindowManager$BadTokenException : Unable to add window -- token android.os.BinderProxy@40b47bd8 is not valid; is your activity running? at android.view.ViewRoot.
Read more >Unable to add window -- token null is not valid; is your activity ...
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@skydoves @sembozdemir So far, I have not been able to reproduce this error in my setup. Very encouraging! Would be great if someone could confirm the fix in a published app.
@skydoves yes, it’s being used in an Activity. The reason why I’m not using
myLifeCycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
is because the custom view is being added and removed from its parent programmatically and when it’s added again after being removed I want to still listening the events, otherwise after theLifecycle.Event.ON_DESTROY
the custom view stops receiving the view model events. I’ll try to use the activity aslifecycleOwner =
. The weird thing is that the custom view is declared inside of the activity and has the same life cycle scope in theory.