The first time the activity is started, dialogs work just fine ... however, when the said activity is started again (that is, via a new Intent), creation of any dialog on Xamarin.Forms's Android backend throws an un-handled exception.
I'm running Android 4.4.3.
Upon further inspection:
06-07 15:48:45.426 I/MonoDroid(20742): UNHANDLED EXCEPTION: Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown.
06-07 15:48:45.426 I/MonoDroid(20742): at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Android.App.Dialog.Show () [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Xamarin.Forms.Platform.Android.AndroidActivity.<SetPage>b__1 (Xamarin.Forms.Page sender, Xamarin.Forms.AlertArguments arguments) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Xamarin.Forms.MessagingCenter+<>c__DisplayClass1`2[Xamarin.Forms.Page,Xamarin.Forms.AlertArguments].<Subscribe>b__0 (System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Xamarin.Forms.MessagingCenter.Send[Page,AlertArguments] (Xamarin.Forms.Page sender, System.String message, Xamarin.Forms.AlertArguments args) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at Xamarin.Forms.Page.DisplayAlert (System.String title, System.String message, System.String accept, System.String cancel) [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): at BugPcl.App+Content+<Content>c__async0.MoveNext () [0x00000] in <filename unknown>:0
06-07 15:48:45.426 I/MonoDroid(20742): --- End of managed exception stack trace ---
06-07 15:48:45.426 I/MonoDroid(20742): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@64e3d7b8 is not valid; is your activity running?
06-07 15:48:45.426 I/MonoDroid(20742): at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
06-07 15:48:45.426 I/MonoDroid(20742): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
06-07 15:48:45.426 I/MonoDroid(20742): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-07 15:48:45.426 I/MonoDroid(20742): at android.app.Dialog.show(Dialog.java:286)
06-07 15:48:45.426 I/MonoDroid(20742): at xamarin.forms.platform.android.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
06-07 15:48:45.426 I/MonoDroid(20742): at xamarin.forms.platform.android.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:29)
06-07 15:48:45.426 I/MonoDroid(20742): at android.view.View.performClick(View.java:4438)
06-07 15:48:45.426 I/MonoDroid(20742): at android.view.View$PerformClick.run(View.java:18422)
06-07 15:48:45.426 I/MonoDroid(20742): at android.os.Handler.handleCallback(Handler.java:733)
06-07 15:48:45.426 I/MonoDroid(20742): at android.os.Handler.dispatchMessage(Handler.java:95)
06-07 15:48:45.426 I/MonoDroid(20742): at android.os.Looper.loop(Looper.java:136)
06-07 15:48:45.426 I/MonoDroid(20742): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-07 15:48:45.426 I/MonoDroid(20742): at java.lang.reflect.Method.invoke(Native Method)
06-07 15:48:45.426 I/MonoDroid(20742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-07 15:48:45.426 I/MonoDroid(20742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-07 15:48:45.436 E/mono (20742):
06-07 15:48:45.436 E/mono (20742): Unhandled Exception:
06-07 15:48:45.436 E/mono (20742): Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown.
06-07 15:48:45.436 E/mono (20742): at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono (20742): at Android.App.Dialog.Show () [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono (20742): at Xamarin.Forms.Platform.Android.AndroidActivity.<SetPage>b__1 (Xamarin.Forms.Page sender, Xamarin.Forms.AlertArguments arguments) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono (20742): at Xamarin.Forms.MessagingCenter+<>c__DisplayClass1`2[Xamarin.Forms.Page,Xamarin.Forms.AlertArguments].<Subscribe>b__0 (System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono (20742): at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono (20742): at Xamarin.Forms.MessagingCenter.Send[Page,AlertArguments] (Xamarin.Forms.Page sender, System.String message, Xamarin.Forms.AlertArguments
06-07 15:48:45.436 E/mono-rt (20742): [ERROR] FATAL UNHANDLED EXCEPTION: Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown.
06-07 15:48:45.436 E/mono-rt (20742): at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono-rt (20742): at Android.App.Dialog.Show () [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono-rt (20742): at Xamarin.Forms.Platform.Android.AndroidActivity.<SetPage>b__1 (Xamarin.Forms.Page sender, Xamarin.Forms.AlertArguments arguments) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono-rt (20742): at Xamarin.Forms.MessagingCenter+<>c__DisplayClass1`2[Xamarin.Forms.Page,Xamarin.Forms.AlertArguments].<Subscribe>b__0 (System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono-rt (20742): at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x00000] in <filename unknown>:0
06-07 15:48:45.436 E/mono-rt (20742): at Xamarin.Forms.MessagingCenter.Send[Page,AlertArguments] (Xamarin.Forms.Page sender, System.String message, Xamarin.Forms.Al
06-07 15:48:45.476 I/ActivityManager( 698): Process Bug2.Bug2 (pid 20742) has died.
It seems like AlertDialog is being fed the wrong Context (should be an activity's Context, not system's context).
[Activity(Label = "Form")]
>
public class Form : AndroidActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); Xamarin.Forms.Forms.Init(this, bundle); SetPage(GetMainPage()); } private static Page GetMainPage() { return new Content(); } private class Content : ContentPage { public Content() { var button = new Xamarin.Forms.Button(); button.Text = "Dialog"; button.VerticalOptions = LayoutOptions.Center; button.Clicked += async (sender, e) => { await DisplayAlert("Test", "Test1212123", "Accept", "Cancel"); }; Content = button; } } }
I simply launch the above code by modifying
button.Click += delegate
{
StartActivity(typeof(Form));
};
In the initial project template for ICS Xamarin.Android application.
I then back press out of the Xamarin.Form's activity, and press that button again to relaunch the Xamarin.Form activity. I then press the Dialog button, and the app crashes/hangs (the latter if the debugger is attached).