致命异常:java.lang.RuntimeException:包裹:无法封送值颜色

问题描述 投票:0回答:1

我有一些数据的数据类:

@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 中得到了这个。

我用谷歌搜索,找不到此案例的任何解决方案。

为什么会出现这种情况以及如何解决?

谢谢。

kotlin android-jetpack-compose
1个回答
0
投票

似乎在代码中的某个时刻,您试图将

ChartModel
Color
本身写入捆绑包中。据我所知,代码中的某些地方可能会发生这种情况:

  • 如果您在经典 Android View 应用程序中使用 Compose,您可能会尝试将
    ChartModel
    Color
    存储在
    onSaveInstanceState
    Bundle
    中。然后,当应用程序即将从 RAM 中被杀死时,就会发生崩溃。
  • 如果您在经典 Android View 应用程序中使用 Compose,您可能会尝试将
    ChartModel
    Color
    存储在
    Bundle
    中,并使用
    Bundle
    startActivity
    传递到下一个 Activity。然后,当应用程序尝试构建
    Bundle
    时,就会发生崩溃。
  • 如果您使用
    rememberSaveable
    ,则要存储的类型需要能够存储在
    Bundle
    中。这意味着该值必须是基本类型、实现
    Parcelable
    或实现
    Serializable

如果这不足以帮助您进行调查,请编辑您的帖子并包含您正在使用

ChartModel
Color
的代码片段。

© www.soinside.com 2019 - 2024. All rights reserved.