OAuthTwoDemo.XForms icon indicating copy to clipboard operation
OAuthTwoDemo.XForms copied to clipboard

Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown

Open BrunoVT1992 opened this issue 10 years ago • 15 comments

Hi, I used your code for my app but when I invoke an action in my android page renderer I get the above error. This is my code:

protected override void OnElementChanged(ElementChangedEventArgs<Page> e) { base.OnElementChanged(e);

        var activity = this.Context as Activity;

        var auth = new OAuth2Authenticator(
            clientId: App.GoogleApiClientId,
            clientSecret: App.GoogleApiClientSecret,
            scope: "email",
            authorizeUrl: new Uri(App.GoogleApiAuthorizeUrl),
            redirectUrl: new Uri(App.GoogleApiRedirectUrl2),
            accessTokenUrl: new Uri(App.GoogleApiAccessTokenUrl),
            getUsernameAsync: null
        );

        auth.Title = App.GetTextFromKey("Login");
        auth.AllowCancel = false;

        auth.Completed += (sender, eventArgs) =>
        {
            if (eventArgs.IsAuthenticated)
            {
                string code = "";
                eventArgs.Account.Properties.TryGetValue("access_token", out code);
                Console.WriteLine("ac: " + code);

                App.GoogleApiAccesToken = code;

                eventArgs.Account.Properties.TryGetValue("refresh_token", out code);
                Console.WriteLine("rc: " + code);

                NavHelper.Nav.NavigateToValidationPageAction.Invoke();
            }
        };

        activity.StartActivity(auth.GetUI(activity));
    }

public Action NavigateToValidationPageAction { get { return new Action(() => { App.LoginOk = true; NavPage.Navigation.PopModalAsync(); } }); } }

BrunoVT1992 avatar Oct 23 '14 13:10 BrunoVT1992

10-23 15:38:04.813 I/MonoDroid(21116): UNHANDLED EXCEPTION: 10-23 15:38:04.853 I/MonoDroid(21116): Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown. 10-23 15:38:04.853 I/MonoDroid(21116): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068> 10-23 15:38:04.853 I/MonoDroid(21116): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00062] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/0ef616ae/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:491 10-23 15:38:04.853 I/MonoDroid(21116): at Android.App.Dialog.Show () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/0ef616ae/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Android.App.Dialog.cs:2497 10-23 15:38:04.853 I/MonoDroid(21116): at Xamarin.Utilities.Android.ActivityEx.ShowError (Android.App.Activity,string,string) <IL 0x00048, 0x001e7> 10-23 15:38:04.853 I/MonoDroid(21116): at Xamarin.Utilities.Android.ActivityEx.ShowError (Android.App.Activity,string,System.Exception) <IL 0x00021, 0x00147> 10-23 15:38:04.853 I/MonoDroid(21116): at Xamarin.Auth.WebAuthenticatorActivity.<OnCreate>m__1 (object,Xamarin.Auth.AuthenticatorErrorEventArgs) <IL 0x00017, 0x000cb> 10-23 15:38:04.853 I/MonoDroid(21116): at Xamarin.Auth.Authenticator/<OnError>c__AnonStorey2.<>m__0 () <IL 0x00024, 0x000cf> 10-23 15:38:04.853 I/MonoDroid(21116): at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/0ef616ae/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36 10-23 15:38:04.853 I/MonoDroid(21116): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/0ef616ae/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Java.Lang.IRunnable.cs:71 10-23 15:38:04.853 I/MonoDroid(21116): at (wrapper dynamic-method) object.bbb4e52b-b564-45a7-a4c2-7012ae96822f (intptr,intptr) <IL 0x00011, 0x0003b> 10-23 15:38:04.853 I/MonoDroid(21116): --- End of managed exception stack trace --- 10-23 15:38:04.853 I/MonoDroid(21116): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d42dc8 is not valid; is your activity running? 10-23 15:38:04.853 I/MonoDroid(21116): android.view.WindowManager$BadTokenEx10-23 15:38:04.863 I/MonoDroid(21116): at android.view.ViewRootImpl.setView(ViewRootImpl.java:705) 10-23 15:38:04.863 I/MonoDroid(21116): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:453) 10-23 15:38:04.863 I/MonoDroid(21116): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351) 10-23 15:38:04.863 I/MonoDroid(21116): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171) 10-23 15:38:04.863 I/MonoDroid(21116): at android.view.Window$LocalWindowManager.addView(Window.java:558) 10-23 15:38:04.863 I/MonoDroid(21116): at android.app.Dialog.show(Dialog.java:282) 10-23 15:38:04.863 I/MonoDroid(21116): at mono.java.lang.RunnableImplementor.n_run(Native Method) 10-23 15:38:04.863 I/MonoDroid(21116): at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29) 10-23 15:38:04.863 I/MonoDroid(21116): at android.app.Activity.runOnUiThread(Activity.java:4649) 10-23 15:38:04.863 I/MonoDroid(21116): at android.app.10-23 15:38:04.863 I/MonoDroid(21116): at mono.java.lang.RunnableImplementor.n_run(Native Method) 10-23 15:38:04.863 I/MonoDroid(21116): at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29) 10-23 15:38:04.863 I/MonoDroid(21116): at android.os.Handler.handleCallback(Handler.java:615) 10-23 15:38:04.863 I/MonoDroid(21116): at android.os.Handler.dispatchMessage(Handler.java:92) 10-23 15:38:04.863 I/MonoDroid(21116): at android.os.Looper.loop(Looper.java:155) 10-23 15:38:04.863 I/MonoDroid(21116): at android.app.ActivityThread.main(ActivityThread.java:5454) 10-23 15:38:04.863 I/MonoDroid(21116): at java.lang.reflect.Method.invokeNative(Native Method) 10-23 15:38:04.863 I/MonoDroid(21116): at java.lang.reflect.Method.invoke(Method.java:511) 10-23 15:38:04.863 I/MonoDroid(21116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 10-23 15:38:04.863 I/MonoDroid(21116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 10-23 15:38:04.863 I/MonoDroid(21116): at com.android.internal.os.ZygoteIn10-23 15:38:04.863 I/MonoDroid(21116): at dalvik.system.NativeStart.main(Native Method) 10-23 15:38:04.873 E/mono-rt (21116): Stacktrace: 10-23 15:38:04.873 E/mono-rt (21116): 10-23 15:38:04.873 E/mono-rt (21116): 10-23 15:38:04.873 E/mono-rt (21116): ================================================================= 10-23 15:38:04.873 E/mono-rt (21116): Got a SIGSEGV while executing native code. This usually indicates 10-23 15:38:04.873 E/mono-rt (21116): a fatal error in the mono runtime or one of the native libraries 10-23 15:38:04.873 E/mono-rt (21116): used by your application. 10-23 15:38:04.873 E/mono-rt (21116): ================================================================= 10-23 15:38:04.873 E/mono-rt (21116): 10-23 15:38:04.873 F/libc (21116): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 21116 (m.randstad.meta) The program 'Mono' has exited with code 0 (0x0).

BrunoVT1992 avatar Oct 23 '14 13:10 BrunoVT1992

What Android API level?

jsauve avatar Oct 23 '14 13:10 jsauve

My app supports android 4.0 and above

BrunoVT1992 avatar Oct 23 '14 14:10 BrunoVT1992

If you ARE using a very recent API level, I'm interested to see if you get the same type of exception when running one of the Xamarin.Forms samples: https://github.com/xamarin/xamarin-forms-samples

Try a few out and see what you get. If you're consistently seeing the same error, it may be a problem with APIs in the newest API level.

Full disclosure: I'm really not an Android expert of any kind (yet).

jsauve avatar Oct 23 '14 14:10 jsauve

Is that 4.0 what you're TARGETING in the app settings? Is that the only API level that you have installed?

jsauve avatar Oct 23 '14 14:10 jsauve

I only have it with this app. The code works in iOS but not android. I have only api level 14 in use.

BrunoVT1992 avatar Oct 23 '14 14:10 BrunoVT1992

When I built this sample, I only had Android API level 18 (Android 4.3 Jellybean) installed. No other API levels were installed.

jsauve avatar Oct 23 '14 14:10 jsauve

Ok, I will try this tomorrow when I am back at work, I will let you know what happens.

BrunoVT1992 avatar Oct 23 '14 14:10 BrunoVT1992

Cool. I'm interested to hear.

jsauve avatar Oct 23 '14 14:10 jsauve

So, I tried different android api levels and I get the same error in all of them. Now I get this error trace:

An unhandled exception occured. 10-24 12:19:58.663 D/libc (11763): pt_debug : pthread_create->start_routine=0x402ad509, tls=0x59a5cf00, arg=0x5d270b40 10-24 12:19:58.663 D/libc (11763): pt_debug : __thread_entry->func=0x402ad509 10-24 12:19:58.663 D/libc (11763): , tls=0x59a5cf00, arg=0x5d270b40 The program 'Mono' has exited with code 0 (0x0).

BrunoVT1992 avatar Oct 24 '14 10:10 BrunoVT1992

So I narrowed the error down to the pagerenderer where I implement the xamarin auth component.

BrunoVT1992 avatar Oct 24 '14 10:10 BrunoVT1992

More specific, it crashes from the moment it loads my redirect url

BrunoVT1992 avatar Oct 27 '14 10:10 BrunoVT1992

anyone found a solution to this. I too getting this error.

asanka-indrajith avatar Feb 02 '15 07:02 asanka-indrajith

Up Up Up :-)

I'm getting the same error with Facebook authentication. It seems to happen on the redirection after the successfull authentication (I received the facebook token).

I get this error with this sample and on my own project. I tried compiling for Android 6 and 7.

Can someone help me with this ??

Pleeeeeeeeeeeeease

davidesan avatar Sep 23 '16 12:09 davidesan

That error went away when I modified my returnUrl by the Facebook one (https://www.facebook.com/connect/login_success.html). Previously I used a custom one. (y) (y) (y)

davidesan avatar Sep 29 '16 11:09 davidesan