Xamarin形成异常:windowmanagerbadtokenexception

时间:2014-08-18 03:20:04

标签: android xamarin xamarin.android xamarin.forms

我在这里发了帖子:http://forums.xamarin.com/discussion/22386/exit-app-through-back-open-up-again-and-click-on-picker-exception#latest但我也在尝试stackoverflow。

我的问题是当我退出并重新启动它时,然后点击一个Picker,抛出一个异常。这是一个非常简单的例子:

    public static Page GetMainPage()
    {
        Picker priorityPicker = new Picker
        {
            Title = "Priority",
            HorizontalOptions = LayoutOptions.Fill
        };

        // Add all possible priorities
        for (int i = 0; i < 5; i++ )
        {
            //priorityPicker.Items.Add(Priority.PriorityToString(priority));
            priorityPicker.Items.Add(i.ToString());
        }

        return new ContentPage
        {
            Content = new StackLayout 
            {
                Children = { priorityPicker }
            },
        };
    }

如果我运行它,然后退出应用程序,然后重新启动应用程序(全部在模拟器中),我得到以下异常:

    08-17 23:18:03.247 I/MonoDroid( 2048): UNHANDLED EXCEPTION: Android.Views.WindowManagerBadTokenException: Exception of type 'Android.Views.WindowManagerBadTokenException' was thrown.
    08-17 23:18:03.247 I/MonoDroid( 2048): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00062] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:490
    08-17 23:18:03.247 I/MonoDroid( 2048): at Android.App.Dialog.Show () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Dialog.cs:2497
    08-17 23:18:03.247 I/MonoDroid( 2048): at Xamarin.Forms.Platform.Android.PickerRenderer.OnClick () <IL 0x0015e, 0x00626>
    08-17 23:18:03.247 I/MonoDroid( 2048): at Xamarin.Forms.Platform.Android.PickerRenderer/PickerListener.OnClick (Android.Views.View) <IL 0x00011, 0x00097>
    08-17 23:18:03.247 I/MonoDroid( 2048): at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Views.View.cs:1840
    08-17 23:18:03.247 I/MonoDroid( 2048): at (wrapper dynamic-method) object.60b39a4e-8626-4d2b-8134-0e521addd16d (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
    08-17 23:18:03.247 I/MonoDroid( 2048): 
    08-17 23:18:03.247 I/MonoDroid( 2048):   --- End of managed exception stack trace ---
    08-17 23:18:03.247 I/MonoDroid( 2048): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b0fe82a0 is not valid; is your activity running?
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.app.Dialog.show(Dialog.java:286)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at xamarin.forms.platform.android.PickerRenderer_PickerListener.n_onClick(Native Method)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at xamarin.forms.platform.android.PickerRenderer_PickerListener.onClick(PickerRenderer_PickerListener.java:29)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.view.View.performClick(View.java:4438)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.view.View$PerformClick.run(View.java:18422)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.os.Handler.handleCallback(Handler.java:733)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.os.Handler.dispatchMessage(Handler.java:95)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.os.Looper.loop(Looper.java:136)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at android.app.ActivityThread.main(ActivityThread.java:5017)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at java.lang.reflect.Method.invoke(Method.java:515)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    08-17 23:18:03.247 I/MonoDroid( 2048):  at dalvik.system.NativeStart.main(Native Method)
    An unhandled exception occured.

奇怪的是,在失败后,我可以重新运行应用程序,然后成功打开选择器。然后,它将再次失败,依此类推。谢谢你的帮助。

编辑:DisplayActionSheet也是如此。

1 个答案:

答案 0 :(得分:1)

原来我要做的就是更新。我没想到这个,因为我刚刚安装了Xamarin,因为VS和Xamarin Studio都没有显示任何更新。

对于遇到此问题的其他人:转到tools \ Nuget Package Manager \ Package manager控制台并输入:update-package xamarin.forms