我有一些数据的数据类:
@Keep
@Parcelize
data class ChartModel(
val value: Float,
val color: @RawValue Color
): Parcelable
Jetpack Compose 中的用法如下所示:
import androidx.compose.ui.graphics.*
...
val chartModel = ChartModel(13.0f, Color(0xFF3EB489))
...
在某个时刻,一旦调用该类,就会发生异常:
Fatal Exception: java.lang.RuntimeException: Parcel: unable to marshal value Color(0.24313726, 0.7058824, 0.5372549, 1.0, sRGB IEC61966-2.1)
at android.os.Parcel.writeValue(Parcel.java:1946)
at com.xxxxxxxx.xxxxxxxx.Slice.writeToParcel(PieChart.kt:17)
at android.os.Parcel.writeParcelable(Parcel.java:1965)
at android.os.Parcel.writeValue(Parcel.java:1871)
at android.os.Parcel.writeList(Parcel.java:1153)
at android.os.Parcel.writeValue(Parcel.java:1893)
at android.os.Parcel.writeList(Parcel.java:1153)
at android.os.Parcel.writeValue(Parcel.java:1893)
at android.os.Parcel.writeMapInternal(Parcel.java:1004)
at android.os.Parcel.writeMap(Parcel.java:986)
at android.os.Parcel.writeValue(Parcel.java:1858)
at android.os.Parcel.writeMapInternal(Parcel.java:1004)
at android.os.Parcel.writeMap(Parcel.java:986)
at android.os.Parcel.writeValue(Parcel.java:1858)
at android.os.Parcel.writeList(Parcel.java:1153)
at android.os.Parcel.writeValue(Parcel.java:1893)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:1036)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)
at android.os.Bundle.writeToParcel(Bundle.java:1304)
at android.os.Parcel.writeBundle(Parcel.java:1105)
at android.os.Parcel.writeValue(Parcel.java:1862)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:1036)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)
at android.os.Bundle.writeToParcel(Bundle.java:1304)
at android.os.Parcel.writeBundle(Parcel.java:1105)
at android.os.Parcel.writeValue(Parcel.java:1862)
at android.os.BaseBundle.dumpStats(BaseBundle.java:1690)
at android.os.BaseBundle.dumpStats(BaseBundle.java:1727)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:150)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
我不知道发生异常时应用程序是否崩溃,因为我在 Crashlitycs 中得到了这个。
我用谷歌搜索,找不到此案例的任何解决方案。
为什么会出现这种情况以及如何解决?
谢谢。
似乎在代码中的某个时刻,您试图将
ChartModel
或 Color
本身写入捆绑包中。据我所知,代码中的某些地方可能会发生这种情况:
ChartModel
或 Color
存储在 onSaveInstanceState
Bundle
中。然后,当应用程序即将从 RAM 中被杀死时,就会发生崩溃。ChartModel
或 Color
存储在 Bundle
中,并使用 Bundle
将 startActivity
传递到下一个 Activity。然后,当应用程序尝试构建 Bundle
时,就会发生崩溃。rememberSaveable
,则要存储的类型需要能够存储在Bundle
中。这意味着该值必须是基本类型、实现Parcelable
或实现Serializable
。如果这不足以帮助您进行调查,请编辑您的帖子并包含您正在使用
ChartModel
或 Color
的代码片段。