I'm using Xamarin for Android and I'm getting this error in an AlertDialog.Builder.Show() method, but it only happens on some Samsung (with Android 7.0) devices, we have tried some other devices and this problem doesn't happen. I'm getting it only after using the application some time. The stacktrace is the following
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0
at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <7802aa64ad574c33adca332a3fa9706a>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <7802aa64ad574c33adca332a3fa9706a>:0
at Android.App.AlertDialog+Builder.Show () [0x0000a] in <dc51acef1f304f0dab449a7fc6039799>:0
at Prizma.Controls.Common.BindingComboBox.ShowDialog () [0x00062] in C:\TeamProjects\PrizmaProject\Main\MobileSales.iOS\Prizma.Controls.iOS\Common\BindingComboBox.cs:408
--- End of managed Android.Views.WindowManagerBadTokenException stack trace ---
android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@cb70704 has already been added
at android.view.ViewRootImpl.setView(ViewRootImpl.java:902)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:97)
at android.app.Dialog.show(Dialog.java:404)
at android.app.AlertDialog$Builder.show(AlertDialog.java:1136)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11185)
at android.view.View$PerformClick.run(View.java:23752)
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:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
My code is something like this
try
{
AlertDialog.Builder a = new AlertDialog.Builder(MainActivity);
a.SetTitle("Select");
a.SetAdapter(_Adapter, new EventHandler<DialogClickEventArgs>(ClosedDialog));
a.Create();
a.Show();
}
catch (Exception ex)
{
//Exception code
}
I put the AlertDialog.Builder Show() method in a Try Catch, but after I get the exception for the first time, I keep getting it everytime.
Some remarks
- I'm still getting this error even the application never goes to the background.
- The app is compiled using the latest Android SDK (8.1)
- I didin't have Xamarin Android to the latest version, but now I have it and the problem is still there.
- Not just the AlertDialogs doesn't show after the error, the Popup menus too.
- I got a Samsung Galaxy J7 Prime with Android 6.0.1 and this error didn't happen, but after upgrade it to 7.0 the problem started. In other devices like Motorola, Hawuei, LG, etc.. we don't have this issue even they have Android 7.0.
Please help me, I have weeks with this issue and my clients that have Samsung devices are killing me :)
Thanks in advance.
Regards
Allan
WindowManagerBadTokenException
is normally a bad context or you are not on the main/ui thread during theShow
, also how you de-registering & disposing that EventHandler that you are new'ing in that .ator, is your adapter using threads? FYI, You do not need to callCreate
as you are not storing the resultingAlertDialog
. – SequestrationisFinishing
as the answer suggested. – Oldworld