简体   繁体   中英

Android.views.windowmanagerbadtokenexception: unable to add window -- token android.os.binderproxy

I am developing an android application using Xamarin.

I am creating custom toast messages similar to android Toast.MakeText() . Check my C# .NET code below:

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 android.os.BinderProxy@901e8d6 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 android.os.BinderProxy@901e8d6 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) }

It 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?

I placed the condition

if (!((Activity)context).IsFinishing)
{
   //Posted code
}

The crashing issue not appearing now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM