Xamarin Android -无法启动活动ComponentInfo{[activityName]}: java.l

本文关键字:activityName java ComponentInfo Android 活动 启动 Xamarin | 更新日期: 2023-09-27 18:03:02

最近,我们的应用程序中出现了以下几个异常:

. lang。运行时异常:无法启动活动ComponentInfo {com.myapp.android/com.myapp.android.WelcomeActivity}:java.lang.RuntimeException:包裹android.os.Parcel@426f0118:在偏移量736处解组未知类型代码2131558952android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2429)在android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2493)在android.app.ActivityThread.access$800(ActivityThread.java:166android.app.ActivityThread H.handleMessage美元(ActivityThread.java: 1283)android.os.Handler.dispatchMessage(Handler.java:102android.os.Looper.loop (Looper.java: 136)android.app.ActivityThread.main (ActivityThread.java: 5584)java.lang.reflect.Method。调用的(原生方法java.lang.reflect.Method.invoke (Method.java: 515)com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 1268)(ZygoteInit.java:1084dalvik.system.NativeStart。main(Native Method)原因:java.lang.RuntimeException:包裹android.os.Parcel@426f0118:在偏移量736处解组未知类型代码2131558952android.os.Parcel.readValue (Parcel.java: 2087)android.os.Parcel.readSparseArrayInternal (Parcel.java: 2382)android.os.Parcel.readSparseArray (Parcel.java: 1742)android.os.Parcel.readValue (Parcel.java: 2077)android.os.Parcel.readArrayMapInternal (Parcel.java: 2321)android.os.Bundle.unparcel (Bundle.java: 249)android.os.Bundle.getSparseParcelableArray (Bundle.java: 1273)com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState (PhoneWindow.java: 1884)在android.app.Activity.onRestoreInstanceState(Activity.java:989android.app.Activity.performRestoreInstanceState (Activity.java: 961)在android.app.Instrumentation.callActivityOnRestoreInstanceState (Instrumentation.java: 1145)

在android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2407)…11个

我检查了我的活动,但没有与可包装的东西有关的东西。所以我挖掘了更多,并在活动中使用的一个小部件中找到了可Parcelable代码。我非常怀疑它可能是由以下代码引起的:

protected override void OnRestoreInstanceState(IParcelable state)
        {
            try
            {
                SavedState savedState = (SavedState)state;
                base.OnRestoreInstanceState(savedState.SuperState);
                mCurrentPage = savedState.CurrentPage;
                mSnapPage = savedState.CurrentPage;
            }
            catch
            {
                base.OnRestoreInstanceState(state);
                // Ignore, this needs to support IParcelable...
            }
            RequestLayout();
        }
        protected override IParcelable OnSaveInstanceState()
        {
            var superState = base.OnSaveInstanceState();
            var savedState = new SavedState(superState)
            {
                CurrentPage = mCurrentPage
            };
            return savedState;
        }
        private class SavedState : BaseSavedState
        {
            public int CurrentPage { get; set; }
            public SavedState(IParcelable superState) : base(superState)
            {
            }
            private SavedState(Parcel parcel) : base(parcel)
            {
                CurrentPage = parcel.ReadInt();
            }
            public override void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
            {
                base.WriteToParcel(dest, flags);
                dest.WriteInt(CurrentPage);
            }
            [ExportField("CREATOR")]
            private static SavedStateCreator InitializeCreator()
            {
                return new SavedStateCreator();
            }
            private class SavedStateCreator : Java.Lang.Object, IParcelableCreator
            {
                public Java.Lang.Object CreateFromParcel(Parcel source)
                {
                    return new SavedState(source);
                }
                public Java.Lang.Object[] NewArray(int size)
                {
                    return new Object[size];
                }
            }
        }

代码实际上取自下面这个例子:https://github.com/xamarin/monodroid-samples/blob/master/ViewPagerIndicator/ViewPagerIndicator/Library/CirclePageIndicator.cs

我个人无法复制这个,但它已经出现在我们的应用程序崩溃报告中。

Xamarin Android -无法启动活动ComponentInfo{[activityName]}: java.l

通过删除所有创建者的东西并坚持基本的修复:

protected override void OnRestoreInstanceState(IParcelable state)
        {
            var bundle = state as Bundle;
            if (bundle != null)
            {
                mCurrentPage = bundle.GetInt("currentPage", 0);
                mSnapPage = mCurrentPage;
                state = (IParcelable)bundle.GetParcelable("superState");
            }
            base.OnRestoreInstanceState(state);
            RequestLayout();
        }
        protected override IParcelable OnSaveInstanceState()
        {
            var bundle = new Bundle();
            bundle.PutParcelable("superState", base.OnSaveInstanceState());
            bundle.PutInt("currentPage", mCurrentPage);
            return bundle;
        }