Repository navigation
Closed
Labels
area/viewsIssue/Discussion/PR that has to do with ViewsIssue/Discussion/PR that has to do with ViewsbugSomething isn't workingSomething isn't working
Description
Is there an existing issue for this?
- I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
I have a .net MAUI app published to Play Store, from appcenter diagnostics I am seeing this exception resulting into crashes in production. I need help to understand why this could be happening and how to fix it.
It's coming whiel trying to show the Popup, PopupExtensions.ShowPopupAsync(). I am not able to reproduce the issue.
Stack trace -
Java.Lang.RuntimeException: Unable to add window -- token android.os.BinderProxy@18889ed is not valid; is your activity running?
Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference , JniObjectReference , JniMethodInfo , JniArgumentValue* )
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* )
Android.App.Dialog.Show()
CommunityToolkit.Maui.Core.Views.MauiPopup.Show()
CommunityToolkit.Maui.Core.Handlers.PopupHandler.MapOnOpened(PopupHandler handler, IPopup view, Object result)
Microsoft.Maui.CommandMapper`2.<>c__DisplayClass6_0[[CommunityToolkit.Maui.Core.IPopup, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Handlers.PopupHandler, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v, Object o)
Microsoft.Maui.CommandMapper.InvokeCore(String key, IElementHandler viewHandler, IElement virtualView, Object args)
Microsoft.Maui.CommandMapper.Invoke(IElementHandler viewHandler, IElement virtualView, String property, Object args)
Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
CommunityToolkit.Maui.Views.PopupExtensions.CreatePopup(Page page, Popup popup)
CommunityToolkit.Maui.Views.PopupExtensions.CreateAndShowPopupAsync[FolderInput](Page page, FolderInput popup)
CommunityToolkit.Maui.Views.PopupExtensions.ShowPopupAsync[FolderInput](Page page, FolderInput popup)
>>MyAppXX.Views.Pages.Home.addXX_tap(Object sender, EventArgs e)
>>MyAppXX.Controls.AqButton.OnGridTapped(Object sender, TappedEventArgs e)
Microsoft.Maui.Controls.TapGestureRecognizer.SendTapped(View sender, Func`2 getPosition)
Microsoft.Maui.Controls.Platform.TapGestureHandler.OnTap(Int32 count, MotionEvent e)
Microsoft.Maui.Controls.Platform.InnerGestureListener.Android.Views.GestureDetector.IOnGestureListener.OnSingleTapUp(MotionEvent e)
Android.Views.GestureDetector.IOnGestureListenerInvoker.n_OnSingleTapUp_Landroid_view_MotionEvent_(IntPtr , IntPtr , IntPtr )
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_Z(_JniMarshal_PPL_Z , IntPtr , IntPtr , IntPtr )
>>android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@d5ab681 is not valid; is your activity running?
android.view.ViewRootImpl.setView ViewRootImpl.java:1400
android.view.WindowManagerGlobal.addView WindowManagerGlobal.java:408
android.view.WindowManagerImpl.addView WindowManagerImpl.java:154
android.app.Dialog.show Dialog.java:361
crc64338477404e88479c.InnerGestureListener.n_onSingleTapUp(Native Method)
crc64338477404e88479c.InnerGestureListener.onSingleTapUp InnerGestureListener.java:80
android.view.GestureDetector.onTouchEvent GestureDetector.java:752
mono.android.view.View_OnTouchListenerImplementor.n_onTouch(Native Method)
mono.android.view.View_OnTouchListenerImplementor.onTouch View_OnTouchListenerImplementor.java:31
android.view.View.dispatchTouchEvent View.java:15055
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3126
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2799
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
android.view.ViewGroup.dispatchTransformedTouchEvent ViewGroup.java:3132
android.view.ViewGroup.dispatchTouchEvent ViewGroup.java:2813
com.android.internal.policy.DecorView.superDispatchTouchEvent DecorView.java:571
com.android.internal.policy.PhoneWindow.superDispatchTouchEvent PhoneWindow.java:1899
android.app.Activity.dispatchTouchEvent Activity.java:4403
androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent WindowCallbackWrapper.java:70
com.android.internal.policy.DecorView.dispatchTouchEvent DecorView.java:529
android.view.View.dispatchPointerEvent View.java:15334
android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent ViewRootImpl.java:6955
android.view.ViewRootImpl$ViewPostImeInputStage.onProcess ViewRootImpl.java:6742
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:6161
android.view.ViewRootImpl$InputStage.onDeliverToNext ViewRootImpl.java:6218
android.view.ViewRootImpl$InputStage.forward ViewRootImpl.java:6184
android.view.ViewRootImpl$AsyncInputStage.forward ViewRootImpl.java:6349
android.view.ViewRootImpl$InputStage.apply ViewRootImpl.java:6192
android.view.ViewRootImpl$AsyncInputStage.apply ViewRootImpl.java:6406
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:6165
android.view.ViewRootImpl$InputStage.onDeliverToNext ViewRootImpl.java:6218
android.view.ViewRootImpl$InputStage.forward ViewRootImpl.java:6184
android.view.ViewRootImpl$InputStage.apply ViewRootImpl.java:6192
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:6165
android.view.ViewRootImpl.deliverInputEvent ViewRootImpl.java:9303
android.view.ViewRootImpl.doProcessInputEvents ViewRootImpl.java:9254
android.view.ViewRootImpl.enqueueInputEvent ViewRootImpl.java:9222
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent ViewRootImpl.java:9442
android.view.InputEventReceiver.dispatchInputEvent InputEventReceiver.java:292
android.os.MessageQueue.nativePollOnce(Native Method)
android.os.MessageQueue.next MessageQueue.java:341
android.os.Looper.loopOnce Looper.java:168
android.os.Looper.loop Looper.java:299
android.app.ActivityThread.main ActivityThread.java:8261
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:559
com.android.internal.os.ZygoteInit.main ZygoteInit.java:954
Expected Behavior
App should not crash.
Steps To Reproduce
Production issue, unable to reproduce.
Link to public reproduction project repository
Production issue, no repro URL
Environment
- .NET MAUI CommunityToolkit: 6.0.0
- OS: Android 9 to Android 13
- .NET MAUI: net7.0-android/8.0.100Anything else?
No response
Metadata
Metadata
Assignees
Labels
area/viewsIssue/Discussion/PR that has to do with ViewsIssue/Discussion/PR that has to do with ViewsbugSomething isn't workingSomething isn't working
Activity
ac-lap commentedon Jan 10, 2024
@bijington this issue I am seeing in production, hence don't have repro. I am looking for guidance on how to fix it.
bijington commentedon Jan 10, 2024
@ac-lap It would be difficult to say without being able to reproduce it or know when it happens, do you know when it happens?
I did a quick google search for the error message which came across this:
https://stackoverflow.com/questions/27087983/unable-to-add-window-token-null-is-not-valid-is-your-activity-running
I don't know whether this helps or not but might be a place to start looking
VladislavAntonyuk commentedon Jan 10, 2024
I see your env is outdated, please try with the latest versions of .NET MAUI and MCT
cat0363 commentedon Jan 11, 2024
@ac-lap , I found a condition that causes the exception below.
This exception occurs if the ShowPopupAsync method is called even though the Page from which the Popup is displayed has already been destroyed.
This is a very crude method, but I verified it below.
You can easily cause this problem by moving the app to the background immediately after the destructor is called and starting another app that consumes a lot of memory.
Since I can't reproduce the same conditions as you, I set a timer in the destructor and call the ShowPopupAsync method.
Isn't there a part where the ShowPopupAsync method is called on a page that has already been destroyed?
cat0363 commentedon Jan 11, 2024
@bijington , If you need a reproduction code, let me know.
To reproduce this problem, you need a situation similar to what I described earlier.
As an aside, this problem occurs even in the latest version.
pictos commentedon Jan 11, 2024
@cat0363 can you check if the
DisconnectHandlermethod is called or if you subscribe toHandlerChangedthe value will be null? For me this is more a edge case that I want to think if we can prevent itcat0363 commentedon Jan 11, 2024
I will upload the reproduction code below first.
https://github.com/cat0363/MauiComm-IssuePopupShow.git
@pictos , I'll post it here when I find out.
cat0363 commentedon Jan 11, 2024
@pictos , In the case where the exception occurred, DisconnectHandler was not being called.
At least the exception occurs when opening the Popup, so before calling the DisconnectHandler method.
The locations where errors occur vary depending on the timing, but we investigated the status of the Activity at the locations below where errors occur frequently.
[src\CommunityToolkit.Maui.Core\Handlers\Popup\PopUpHandler.android.cs]
Before calling the Show method above, I inserted the code below to check the status of the Activity.
The value of IsFinishing was True in cases where an exception occurred, and the value of IsFinishing was False in cases where an exception did not occur.
If I were to add a guard condition, it would look like this:
[src\CommunityToolkit.Maui.Core\Handlers\Popup\PopUpHandler.android.cs]
Exceptions occur infrequently in other places as well, so they cannot be fully enumerated.
As far as I investigated, the exception also occurs when creating the MauiPopup below.
[src\CommunityToolkit.Maui.Core\Handlers\Popup\PopUpHandler.android.cs]
By the way, if an exception occurs at the above location, a Null Pointer Exception has occurred.
In the latter case I described, there seems to be no other way than to throw an exception.
This is because an exception occurs in MauiPopup's constructor.
bijington commentedon Jan 16, 2024
Amazing work @cat0363 I've removed the labels so the bot won't close it.
darren-kay commentedon Dec 4, 2024
We have the same error occasionally with popups and app going into the background. Is this go onto the backlog?
Using CommunityToolkit.Maui Version 9.0.2.
Unhandled Exception
Unable to add window -- token android.os.BinderProxy@9d67775 is not valid; is your activity running?
at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference , JniObjectReference , JniMethodInfo , JniArgumentValue* )
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String , IJavaPeerable , JniArgumentValue* )
at Android.App.Dialog.Show()
at CommunityToolkit.Maui.Core.Views.MauiPopup.Show()
at CommunityToolkit.Maui.Core.Handlers.PopupHandler.MapOnOpened(PopupHandler handler, IPopup view, Object result)
at Microsoft.Maui.CommandMapper`2.<>c__DisplayClass6_0[[CommunityToolkit.Maui.Core.IPopup, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[CommunityToolkit.Maui.Core.Handlers.PopupHandler, CommunityToolkit.Maui.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].b__0(IElementHandler h, IElement v, Object o)
at Microsoft.Maui.CommandMapper.InvokeCore(String key, IElementHandler viewHandler, IElement virtualView, Object args)
at Microsoft.Maui.CommandMapper.Invoke(IElementHandler viewHandler, IElement virtualView, String property, Object args)
at Microsoft.Maui.Handlers.ElementHandler.Invoke(String command, Object args)
at CommunityToolkit.Maui.Views.PopupExtensions.CreatePopup(Page page, Popup popup)
at CommunityToolkit.Maui.Views.PopupExtensions.CreateAndShowPopupAsync[Popup](Page page, Popup popup, CancellationToken token)
at CommunityToolkit.Maui.Views.PopupExtensions.ShowPopupAsync[Popup](Page page, Popup popup, CancellationToken token)
at IceHub.Maui.Shared.Services.PopupService.<>c__DisplayClass3_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at IceHub.Maui.Shared.Services.PopupService.ShowPopupWithResult(Popup popup)
at IceHub.Picking.Services.DialogService.<>c__DisplayClass4_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Maui.ApplicationModel.MainThread.<>c__DisplayClass5_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
bijington commentedon Dec 4, 2024
Are you trying to show a popup while the app is backgrounded?
darren-kay commentedon Dec 5, 2024
We have caught this exception in the wild (business app in stores), and from the stack trace, we believe that is the case.
RahmatilloAbduqodirov commentedon Dec 6, 2024
Hi all.
I have the same issue. I found it on Sentry.
I am using: net8.0, CommunityToolkit.Maui-9.0.2.
How do I fix it?
Has anyone found a solution or workaround?
Thanks.
cat0363 commentedon Dec 6, 2024
It's a crude method, but if this problem occurs when the app is running in the background, you can at least avoid the crash by surrounding the Popup display process with a try-catch. In that case, the Popup will never be displayed.
This problem occurs when the Page that displays the Popup is destroyed during the process of displaying the Popup.
Errors can occur in various places because the page on which the popup is displayed is destroyed at an unexpected time.
As far as I investigated before, this problem occurred at such timing.
If you are using Popup for error notifications, you will need to devise measures such as displaying Popup when the app returns to the foreground. Interactive popup display is not suitable where this exception occurs.
If you put a guard condition in the place I posted before, you can reduce the frequency of exceptions, but since exceptions occur even in MauiPopup's constructor, this cannot be completely resolved.