BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@cb70704 has already been added On Samsung devices
Asked Answered
T

4

9

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

Triciatrick answered 19/6, 2018 at 23:3 Comment(6)
WindowManagerBadTokenException is normally a bad context or you are not on the main/ui thread during the Show, 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 call Create as you are not storing the resulting AlertDialog.Sequestration
Thanks @Sequestration for the answer. I'll consider your comments. But the strange thing about this issue is that everything works ok until the first failure every dialog o PopupMenu throws this exception on the Show() method. I don't have so much experience in Xamarin/Android development, but I think as you say the problem is related to the context, but I always use the same activity for the context and the application never goes to the background.Triciatrick
@Triciatrick Were you able to resolve this issue?Oldworld
Hi @Oldworld I haven't resolved this issue, have you? ThanksTriciatrick
@Triciatrick No I haven't resolved. I have tried adding isFinishing as the answer suggested.Oldworld
@Triciatrick Did you end up figuring out the fix? I am having the same issue as above? Any help would be much appreciated.Uncloak
M
4

You are adding a dialog to a dead activity, so before showing dialog, you should check if the activity is already finished by this:


        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle(R.string.app_name);
        builder.setMessage(msg);
        AlertDialog alert = builder.create();
        //To check if activity is finished
        if (!((Activity)ctx).isFinishing()) {
            alert.show();
        }

Mcwilliams answered 21/7, 2019 at 4:0 Comment(0)
R
0

Show your dialog like this in a fragment: use if (!IsFinishing) in an activity

if (!((Activity)Context).IsFinishing) 
                {
 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
            }
}
Renfrow answered 20/6, 2018 at 8:27 Comment(1)
Hi, thanks. I tried that and the exception still ocurs and IsFinishing is false. Thanks.Triciatrick
W
0
try
{
    RunOnUiThread (() => {
          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
}

with reference to : https://forums.xamarin.com/discussion/128420/badtokenexception-unable-to-add-window-window-android-view-viewrootimpl-has-already-been-added

Wallis answered 18/2, 2020 at 14:7 Comment(1)
it is good to explain what you are doing and the content of the llink, for if the resource you are linking every goes deadCosine
R
0

I know its 6 years since this question was posted. But it seems like this is a bug with the Nougat on this Samsung device. Because we are also seeing this very same error only happening on this specific device (Galaxy J7 2016) and that specific (old) version of Android (7.1.1). No other Android versions caused this issue. So we can safely ignore this error, since the code does not point to any particular line and incindence is insignificant.

Revolve answered 19/2, 2024 at 13:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.