简体   繁体   中英

Passing array of objects from activity to service via intent

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM