我不知道是什么原因和什么原因在片段屏幕之一上导致此异常。

我已经尝试使用此TooLargeTool对其进行调试
我有这样的输出:

2-19 18:20:24.317 25554 25554 D TooLargeTool: * android:support:fragments = 23.9 KB
12-19 18:20:24.317 25554 25554 D TooLargeTool: * android:view_state = 0.1 KB
12-19 18:20:24.317 25554 25554 D TooLargeTool: * android-support-nav:fragment:defaultHost = 0.1 KB
12-19 18:20:28.735 25554 25554 D TooLargeTool: MainActivity.onSaveInstanceState wrote: Bundle@264718100 contains 3 keys and measures 2,554.3 KB when serialized as a Parcel
12-19 18:20:28.735 25554 25554 D TooLargeTool: * android:fragments = 0.4 KB
12-19 18:20:28.735 25554 25554 D TooLargeTool: * android:viewHierarchyState = 4.0 KB
12-19 18:20:28.735 25554 25554 D TooLargeTool: * android:support:fragments = 2,549.9 KB
12-19 18:20:28.736 25554 25554 D TooLargeTool: BillingFragment.onSaveInstanceState wrote: Bundle@217680829 contains 0 keys and measures 0.0 KB when serialized as a Parcel
12-19 18:20:28.759 25554 25554 D TooLargeTool: NavHostFragment.onSaveInstanceState wrote: Bundle@148914 contains 4 keys and measures 25.1 KB when serialized as a Parcel
12-19 18:20:28.759 25554 25554 D TooLargeTool: * android-support-nav:fragment:navControllerState = 0.9 KB
12-19 18:20:28.759 25554 25554 D TooLargeTool: * android:support:fragments = 23.9 KB
12-19 18:20:28.759 25554 25554 D TooLargeTool: * android:view_state = 0.1 KB
12-19 18:20:28.759 25554 25554 D TooLargeTool: * android-support-nav:fragment:defaultHost = 0.1 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: MainActivity.onSaveInstanceState wrote: Bundle@143078438 contains 6 keys and measures 1,432.2 KB when serialized as a Parcel
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:fragments = 0.4 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:support:request_fragment_who = 0.1 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:support:fragments = 1,427.5 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:viewHierarchyState = 4.0 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:support:request_indicies = 0.1 KB
12-19 18:20:37.247 25554 25554 D TooLargeTool: * android:support:next_request_index = 0.1 KB
12-19 18:20:37.248 25554 25554 D TooLargeTool: BillingFragment.onSaveInstanceState wrote: Bundle@249100391 contains 0 keys and measures 0.0 KB when serialized as a Parcel
12-19 18:20:37.282 25554 25554 D TooLargeTool: NavHostFragment.onSaveInstanceState wrote: Bundle@79386132 contains 4 keys and measures 25.1 KB when serialized as a Parcel
12-19 18:20:37.282 25554 25554 D TooLargeTool: * android-support-nav:fragment:navControllerState = 0.9 KB
12-19 18:20:37.282 25554 25554 D TooLargeTool: * android:support:fragments = 23.9 KB
12-19 18:20:37.282 25554 25554 D TooLargeTool: * android:view_state = 0.1 KB
12-19 18:20:37.282 25554 25554 D TooLargeTool: * android-support-nav:fragment:defaultHost = 0.1 KB


该屏幕与其他屏幕没有太大区别,也没有这种例外。

我经常有这样的错误

 java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1432340 bytes
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3781)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: android.os.TransactionTooLargeException: data parcel size 1432340 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:615)
        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3636)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3773)


我在其中具有ImageView的自定义视图,并且我认为这种情况可能会将这样的图像保存在Parcel中,如下所示:

internal class SavedState : ValidableTextInput.SavedState {

        var iconState : Drawable? = null

        constructor(superState: Parcelable) : super(superState) {}
        private constructor(parcel: Parcel) : super(parcel) {
            // order of reading must match order of writing
            val bitmap = parcel.readParcelable(javaClass.classLoader) as Bitmap
            this.iconState = BitmapDrawable(Resources.getSystem(), bitmap)
        }

        override fun writeToParcel(out: Parcel, flags: Int) {
            super.writeToParcel(out, flags)

            val bitmap = iconState?.toBitmap()
            out.writeParcelable(bitmap, flags)
        }

        companion object CREATOR : Parcelable.Creator<ValidableIconTextInput.SavedState> {
            override fun createFromParcel(parcel: Parcel): ValidableIconTextInput.SavedState {
                return ValidableIconTextInput.SavedState(parcel)
            }

            override fun newArray(size: Int): Array<ValidableIconTextInput.SavedState?> {
                return arrayOfNulls(size)
            }
        }
    }

最佳答案

我发现它具有SavedState的超类,然后我还将背景可绘制对象保存到了包裹中。无疑这引起了问题。

像这儿:

internal open class SavedState : View.BaseSavedState {

        var textInputState: String? = null
        var backgroundState: Drawable? = null

        constructor(superState: Parcelable) : super(superState) {}
        protected constructor(parcel: Parcel) : super(parcel) {
            // order of reading must match order of writing
            this.textInputState = parcel.readString()
            val bitmap = parcel.readParcelable(javaClass.classLoader) as Bitmap
            this.backgroundState = BitmapDrawable(Resources.getSystem(), bitmap)
        }

        override fun writeToParcel(out: Parcel, flags: Int) {
            super.writeToParcel(out, flags)
            // order of writing must match order of reading
            out.writeString(this.textInputState)
            val bitmap = backgroundState?.toBitmap()
            out.writeParcelable(bitmap, flags)
        }

        override fun describeContents(): Int {
            return 0
        }

        companion object CREATOR : Parcelable.Creator<SavedState> {
            override fun createFromParcel(parcel: Parcel): SavedState {
                return SavedState(parcel)
            }

            override fun newArray(size: Int): Array<SavedState?> {
               return arrayOfNulls(size)
            }
        }
    }

10-08 16:26