3

我不知道在我的一个片段屏幕上导致此异常的原因和位置。

我试图用这个 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)
            }
        }
    }
4

1 回答 1

0

我发现我有 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)
            }
        }
    }
于 2018-12-19T17:43:45.110 回答