Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy

Hi,

   I am developing an android application using xamarin.

   I am creating custom toast messages similar to android Toast.MakeText().please see my c#.net below code

   var @params = new WindowManagerLayoutParams(
                                                 ViewGroup.LayoutParams.WrapContent,
                                                 ViewGroup.LayoutParams.WrapContent,
                                                 WindowManagerTypes.Toast,
                                                 WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchable,
                                                 Android.Graphics.Format.Translucent);

                                    Android.Content.Res.Configuration config = activity.Resources.Configuration;

                                    @params.Gravity = Gravity.GetAbsoluteGravity((GravityFlags.CenterHorizontal | GravityFlags.Bottom), (GravityFlags)config.LayoutDirection);
                                    @params.X = 0;
                                    @params.Y = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 64f, activity.Resources.DisplayMetrics);
                                    @params.WindowAnimations = Android.Resource.Style.AnimationToast;
                                    @params.PackageName = activity.PackageName;
                                    @params.Token = activity.Window.Attributes.Token;

                                    if ((@params.Gravity & GravityFlags.HorizontalGravityMask) == GravityFlags.FillHorizontal)
                                    {
                                        @params.HorizontalWeight = 1.0f;
                                    }

                                    if ((@params.Gravity & GravityFlags.VerticalGravityMask) == GravityFlags.FillVertical)
                                    {
                                        @params.VerticalWeight = 1.0f;
                                    }

                                    try
                                    {
                                        activity.WindowManager.AddView(msg.MView, @params);
                                    }
                                    catch(System.Exception ex)
                                    {
                                    }

While adding the view to Windowmanager I am getting the following exception

{Android.Views.WindowManagerBadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <3cb12bce834a498199b2be6cd3e00922>:0
at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in :0
at Android.Views.IWindowManagerInvoker.AddView (Android.Views.View view, Android.Views.ViewGroup+LayoutParams params) [0x0007c] in :0
at GSP.Phone.Controls.ToastCompat+<>c__DisplayClass11_0.b__0 () [0x00162] in ToastCompat.cs:152
--- End of managed Android.Views.WindowManagerBadTokenException stack trace ---
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:920)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:377)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:105)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
}

seems to me while assiging the token to window manager params is the problem. am I assigning the right token value(activity.Window.Attributes.Token) to window manager params?
Please help me how to resolve the Android.Views.WindowManagerBadTokenException

Thank you,
K.Nagendra Babu

Answers

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    You could refer to the answer: Unable to add window — token android.os.BinderProxy is not valid; is your activity running?

    As @Peter Pascale said:

    This can occur when you are showing the dialog for a context that no longer exists. A common case - if the 'show dialog' operation is after an asynchronous operation, and during that operation, the original activity (that is to be the parent of your dialog) is destroyed.

    You could resolved it via:

    if (!((Activity)context).IsFinishing)
    {
           //show dialog
    }
    
  • KorlakuntaBabuKorlakuntaBabu USMember ✭✭

    Thank you for your response

    I placed the condition

    if (!((Activity)context).IsFinishing)
    {
    //show dialog
    }

    The crashing issue not appearing now

  • mk18576mk18576 Member ✭✭

    where did you place
    if (!((Activity)context).IsFinishing)
    {
    //show dialog
    }

  • KorlakuntaBabuKorlakuntaBabu USMember ✭✭
    edited March 2019
                                   Please check my posted code above
                                   Here I placed 
                                        try
                                        {
                                               if (!activity.IsFinishing)
                                               {
                                                   activity.WindowManager.AddView(msg.MView, @params);
                                               }
                                        }
                                        catch(System.Exception ex)
                                        {
                                        }
    
  • BadamSujithKumarBadamSujithKumar USMember ✭✭

    @KorlakuntaBabu Hi, Nagendra Babu I do need the toast message So I have followed the same as of you but I am getting the same exception even after kepping activity.IsFinishing. Could you plz post your code so that it might help for me your help will be appreciated

  • KorlakuntaBabuKorlakuntaBabu USMember ✭✭

    Hi Sujith,

                Please find attached ToastCompact.txt file, it has custom toast implementation.
    
                Sample toast message call as follows
                ToastCompat.MakeText(this, "Custom toast message.", ToastLength.Long).Show();
    
Sign In or Register to comment.