0

I'm struggling how can I pass an array with objects from activity to service by intent. After debugging i can say that my service doesnt even start because of error java.lang.RuntimeException: Parcel: unable to marshal value (Item(id=0, data=2023-01-02T02:07:11.051, message=123, isValid=false.

 listViewModel.itemList.observe(this) {
        listAdapter.setList(it)
        recyclerView.adapter = listAdapter
        val i = Intent(this, FileService::class.java)
        val bundle = Bundle()
        bundle.putSerializable("data", it)
        i.putExtras(bundle)
        startService(i)
    }

and my data class

@Serializable
data class Item(val id: Int,
                val data: LocalDateTime? = Clock.System.now().toLocalDateTime(TimeZone.UTC),
                val message: String,
                var isValid: Boolean? = false)

As you can see im not using parcelable. I have no idea what to do. I have tried option without bundle.

1
  • 1
    @Serializable is a kotlin serializable. putSerializable requires a Java Serializable. They aren't the same thing. You need to import java.io.serializable, and you need to externd from the Serializable interface. Commented Jan 2, 2023 at 2:13

1 Answer 1

2

You are getting this error because of you used @Serializable instead of Serializable class.

You need to make Item class Serializable. To make Item class serializable I had to add : Serializable at the end of class to make is Serializable. Just like this

class ExampleClass(val price: Double, val discountedPrice: Double) : Serializable

Make sure to import Serializable like this

import java.io.Serializable
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.