How to parcel value with type Any? using @Parcelize
Asked Answered
C

1

15

I have encountered with a problem when trying to put value with type Any? into parcel. I'm using @Parcelize from kotlinx.android.parcel. Lint is warning me and hints with message to add @RawValue, but it's not helping. I got:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling

My data class:

@Parcelize
    data class FormulaNode(
            val term: @RawValue Any? = null,
            val operator: String? = null,
            val left: FormulaNode? = null,
            val right: FormulaNode? = null) : Parcelable
Cashandcarry answered 2/11, 2018 at 10:21 Comment(9)
You cannot parcel anything which is not parcelable. So instead Any use Parcelable.Improbable
@Improbable But I need something like Any, because term can be String or FormulaNode at the same timeCashandcarry
@Improbable Ok, I'll try this approachCashandcarry
@Improbable It's not working in my case, but thanks for helpCashandcarry
@Cashandcarry got any solution ?Marnie
@JithishPN yup, I have started to put into parcel and get from it by my self, with several checks. This was only solution I found in my caseCashandcarry
@Cashandcarry please share referal links to do parcel in kotlin ?Marnie
@JithishPN I have no links, it was my solution. You need to implement parcelable by yourself without @Parcelize.Cashandcarry
@Cashandcarry got solution. if raw value(any) is custom class , implement custom class also Parcelable will solve the issue.Marnie
B
10

Follow this answer: https://mcmap.net/q/273361/-rawvalue-annotation-is-not-applicable-to-target-value-parameter

var tokenType: @RawValue Any? = null
Bacterium answered 27/8, 2020 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.