我有一个Xamarin Forms应用程序,目前正在为Android构建。我有MainActivity
用于扩展FormsApplicationActivity
,但由于我想使用自定义主题,我必须将其更改为FormsAppCompatActivity
(请参阅我的其他问题:Xamarin Forms custom theme not working )。
自从FormsApplicationActivity
更改为FormsAppCompatActivity
后,只要我退出应用程序然后返回应用程序,应用就会崩溃。它在App.xaml.cs
方法的OnResume
类中引发错误,我尝试将MainPage设置为新的导航页面:
protected override void OnResume()
{
bool isRegistered = _authenticationService.IsRegistered();
MainPage = isRegistered
? new NavigationPage(new LoginPage())
: new NavigationPage(new RegisterPage()); // this results in the crash
}
我得到的错误是:
Java.Lang.IllegalStateException:之后无法执行此操作 的onSaveInstanceState
这是堆栈跟踪:
03-09 13:43:52.098 I/MonoDroid( 1243): Java.Lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-09 13:43:52.098 I/MonoDroid( 1243): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
03-09 13:43:52.098 I/MonoDroid( 1243): at Android.Runtime.JNIEnv.CallIntMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:386
03-09 13:43:52.098 I/MonoDroid( 1243): at Android.Support.V4.App.FragmentTransactionInvoker.Commit () [0x00033] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SwitchContentAsync (Xamarin.Forms.Page view, Boolean animated, Boolean removed, Boolean popToRoot) [0x000e1] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnPushAsync (Xamarin.Forms.Page view, Boolean animated) [0x00000] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.PushViewAsync (Xamarin.Forms.Page page, Boolean animated) [0x00000] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.<OnElementChanged>b__13_0 (Xamarin.Forms.Page p) [0x00000] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.EnumerableExtensions.ForEach[T] (IEnumerable`1 enumeration, System.Action`1 action) [0x00010] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1 e) [0x001af] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (Xamarin.Forms.Platform.Android.TElement element) [0x000fc] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer (Xamarin.Forms.VisualElement view) [0x00000] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild (Xamarin.Forms.Page page, Boolean layout) [0x00015] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x00090] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0001a] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.AppOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x0001e] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00012] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Application.set_MainPage (Xamarin.Forms.Page value) [0x0008b] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Smartbit.App.OnResume () [0x00020] in C:\Users\leonc\Documents\Visual Studio 2015\Projects\Smartbit\Smartbit\Smartbit\App.xaml.cs:52
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Application.SendResume () [0x00006] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnStateChanged () [0x00039] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.OnRestart () [0x00019] in <filename unknown>:0
03-09 13:43:52.098 I/MonoDroid( 1243): at Android.App.Activity.n_OnRestart (IntPtr jnienv, IntPtr native__this) [0x00009] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:4539
03-09 13:43:52.098 I/MonoDroid( 1243): at (wrapper dynamic-method) System.Object:cba7a870-1435-4f70-9059-e10915aba0c0 (intptr,intptr)
03-09 13:43:52.098 I/MonoDroid( 1243): --- End of managed exception stack trace ---
03-09 13:43:52.098 I/MonoDroid( 1243): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-09 13:43:52.098 I/MonoDroid( 1243): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1448)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1466)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
03-09 13:43:52.098 I/MonoDroid( 1243): at md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.n_onRestart(Native Method)
03-09 13:43:52.098 I/MonoDroid( 1243): at md5b60ffeb829f638581ab2bb9b1a7f4f3f.FormsAppCompatActivity.onRestart(FormsAppCompatActivity.java:86)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1181)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.Activity.performRestart(Activity.java:5291)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.Activity.performResume(Activity.java:5302)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.os.Handler.dispatchMessage(Handler.java:102)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.os.Looper.loop(Looper.java:136)
03-09 13:43:52.098 I/MonoDroid( 1243): at android.app.ActivityThread.main(ActivityThread.java:5001)
03-09 13:43:52.098 I/MonoDroid( 1243): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:43:52.098 I/MonoDroid( 1243): at java.lang.reflect.Method.invoke(Method.java:515)
03-09 13:43:52.098 I/MonoDroid( 1243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-09 13:43:52.098 I/MonoDroid( 1243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-09 13:43:52.098 I/MonoDroid( 1243): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我有类似的问题。关于它的几点意见:
1)将您的MainPage设置代码移动到App构造函数。因为MainPage应该在OnCreate方法中设置。
public App () {
MainPage = isRegistered
? new NavigationPage(new LoginPage())
: new NavigationPage(new RegisterPage());
}
2)您需要在OnCreate完成之前设置根页面。由于您不知道所需的页面,您可以设置空白页面。
public App () {
MainPage = new ContentPage();
}
3)如果这没有帮助。你可以简单地添加try / catch块比比皆是的MainPage setter。这是一个肮脏的解决方案,但它的工作在AppCompatActivity中,xamarin使用Fragments而不是Views。当android尝试恢复片段状态时,会发生崩溃。在普通的android上我们曾经使用CommitAllowingStateLoss()。
try{
MainPage = isRegistered
? new NavigationPage(new LoginPage())
: new NavigationPage(new RegisterPage());
}catch(Exception){}
答案 1 :(得分:1)
我通过在设置MainPage之前在我的OnResume方法中添加线程sleep来解决它。我的OnResume现在看起来像这样:
protected override void OnResume()
{
base.OnResume();
Task.Delay(10).Wait();
bool isRegistered = _authenticationService.IsRegistered();
MainPage = isRegistered
? new NavigationPage(new LoginPage())
: new NavigationPage(new RegisterPage());
}
有关此解决方法,请参阅this bug report。