java.lang.IllegalStateException:在Xamarin.Forms上的onSaveInstanceState之后无法执行此操作

时间:2016-04-20 16:46:14

标签: android xamarin xamarin.android xamarin.forms

我有这段代码并且间歇性地,我的部署由于IllegalStateException而失败:

它在app.cs中失败了:

void appInitializer_OnDisplayOffers(object sender, System.EventArgs e)
{
    Device.BeginInvokeOnMainThread(() => {
        Xamarin.Forms.Application.Current.MainPage = App.MainLandingPage(false);
    });
}

其中:

public static Page MainLandingPage(bool isReceiptSubmitSuccess = false)
        {
            if (Navigation != null && Navigation.ModalStack.Count == 1)
                Navigation.PopModalAsync();
            return new RootPage();
        }

以下是有关其触发位置的一些信息:

protected override void OnAppearing()
{
    base.OnAppearing();

    timer.Enabled = true;
    timer.AutoReset = false;
    timer.Elapsed += timer_Elapsed;
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    timer.Enabled = false;
    appInitializer.OnDisplayOffers += appInitializer_OnDisplayOffers;

    appInitializer.ValidateLogin();
}

其中:

  // Timer has been added to delay loading of the next screen
  System.Timers.Timer timer = new System.Timers.Timer(2000);

以下是例外情况:

04-20 12:42:26.304 I/MonoDroid( 2886): UNHANDLED EXCEPTION:
04-20 12:42:26.304 I/MonoDroid( 2886): Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-20 12:42:26.304 I/MonoDroid( 2886):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Android.Runtime.JNIEnv.CallIntMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:386 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Android.Support.V4.App.FragmentTransactionInvoker.Commit () [0x00033] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin.Forms.Page view, Boolean animated, Boolean removed, Boolean popToRoot) [0x0013f] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync (Xamarin.Forms.Page view, Boolean animated) [0x00000] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync (Xamarin.Forms.Page page, Boolean animated) [0x00000] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.<OnElementChanged>b__13_0 (Xamarin.Forms.Page p) [0x00000] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.EnumerableExtensions.ForEach[T] (IEnumerable`1 enumeration, System.Action`1 action) [0x00010] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1 e) [0x001af] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (Xamarin.Forms.Platform.Android.TElement element) [0x000fc] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.MasterDetailContainer.set_ChildView (Xamarin.Forms.VisualElement value) [0x0003d] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer.UpdateDetail () [0x0001d] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.MasterDetailPageRenderer.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x0012c] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement view) [0x00000] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild (Xamarin.Forms.Page page, Boolean layout) [0x00015] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x00090] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0001a] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.AppOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x0001e] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Xamarin.Forms.Application.set_MainPage (Xamarin.Forms.Page value) [0x0008b] in <filename unknown>:0 
04-20 12:42:26.304 I/MonoDroid( 2886):   at ProjectName.Core.UI.XamarinForms.LaunchPage+<>c.<appInitializer_OnDisplayOffers>b__6_0 () [0x00001] in C:\Users\~\Documents\ProjectNameMobile4\ProjectName\main\Mobile\ProjectName.Core\UI\XamarinForms\App.cs:312 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36 
04-20 12:42:26.304 I/MonoDroid( 2886):   at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) [0x00009] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:71 
04-20 12:42:26.304 I/MonoDroid( 2886):   at (wrapper dynamic-method) System.Object:b6e56cfb-0162-4eab-9d72-3563aee71861 (intptr,intptr)
 --- End of managed exception stack trace --- 
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1448)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1466)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
04-20 12:42:26.304 I/MonoDroid( 2886):  at mono.java.lang.RunnableImplementor.n_run(Native Method)
04-20 12:42:26.304 I/MonoDroid( 2886):  at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.os.Handler.handleCallback(Handler.java:733)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.os.Handler.dispatchMessage(Handler.java:95)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.os.Looper.loop(Looper.java:136)
04-20 12:42:26.304 I/MonoDroid( 2886):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 12:42:26.304 I/MonoDroid( 2886):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:42:26.304 I/MonoDroid( 2886):  at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:42:26.304 I/MonoDroid( 2886):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 12:42:26.304 I/MonoDroid( 2886):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 12:42:26.304 I/MonoDroid( 2886):  at dalvik.system.NativeStart.main(Native Method)

我的项目不使用任何类型的任何片段或片段管理器。这发生在Xamarin.Forms for Android中。

我应该提一下重启Visual Studio,模拟器或通常重建&#34;修复&#34;一点点直到问题再次随机发生。我的团队对此很难过,他们只是处理它。我很好奇它为什么会发生以及是否有解决方案。

1 个答案:

答案 0 :(得分:0)

我不确定这个问题的真正解决方案是什么,但我建议看两件事。

首先在我见过的每个计时器示例中都使用了它,就像这样的设备计时器。

Device.StartTimer(TimeSpan.FromSeconds(4), SaveChangesTick);

这似乎也适合你所看到的描述。 Link