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

KorlakuntaBabuKorlakuntaBabu USMember ✭✭
edited August 2018 in Xamarin.Android

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 <fcbf47a04b2e4d90beafbae627e1fca4>: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 <ff199c98c3f84852925dd37a0ef6113a>:0 
  at Android.Views.IWindowManagerInvoker.AddView (Android.Views.View view, Android.Views.ViewGroup+LayoutParams params) [0x0007c] in <ff199c98c3f84852925dd37a0ef6113a>:0 
  at GSP.Phone.Controls.ToastCompat+<>c__DisplayClass11_0.<createToastView>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 assigning 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

Sign In or Register to comment.