https://kotlinlang.org logo
#announcements
Title
# announcements
s

sqia09

09/13/2017, 9:56 AM
Hi, I can't seem to pass my parcelable arrays to other activity, so is there anyone who can help me out/
g

gildor

09/13/2017, 9:59 AM
Parcelable array? Array of parcelables?
Could you please show your code
s

sqia09

09/13/2017, 10:01 AM
data class RoutesResponse(@SerializedName("result") val result: String, @SerializedName("freason") val freason: String, @SerializedName("route") var routesList: MutableList<RouteObject>, @SerializedName("route_site") var routeSiteList: MutableList<RouteSite>) : Parcelable { companion object { @JvmField val CREATOR: Parcelable.Creator<RoutesResponse> = object : Parcelable.Creator<RoutesResponse>{ override fun createFromParcel(p0: Parcel): RoutesResponse = RoutesResponse(p0) override fun newArray(p0: Int): Array<RoutesResponse?> = arrayOfNulls(p0) } } constructor(source: Parcel) : this(source.readString(), source.readString(), source.createTypedArrayList(RouteObject.CREATOR), source.createTypedArrayList(RouteSite.CREATOR)) override fun describeContents() = 0 override fun writeToParcel(p0: Parcel?, p1: Int) { p0?.let { with(it){ writeString(result) writeString(freason) writeTypedList(routesList) writeTypedList(routeSiteList) } } } }
the other two models of type RouteSite and RouteObject have similar parcelable implementation
I'm sending the list like this:
val testList = getRelevantRoutes(it.id) toast (testList.size.toString()) startActivity(intentFor<GatewayActivity>(KEY_ROUTE_SITES to testList))
and recieving using this: val siteListFromIntent = intent.getParcelableArrayExtra("routesSiteList")[0]
I'm geting source.readString can not be null when i try to read from intent. I'll drop using anko if there's a work around
g

gildor

09/13/2017, 10:07 AM
So what is error in your case?
Maybe source.readString is really null
s

sqia09

09/13/2017, 10:09 AM
how can i fix it?
java.lang.IllegalStateException: source.readString() must not be null
g

gildor

09/13/2017, 10:12 AM
So string is null, it means that Parcel has no string in first position
s

sqia09

09/13/2017, 10:14 AM
but the list is showing up in the activity from where I want to send my list
g

gildor

09/13/2017, 10:15 AM
Try to debug your intent
s

sqia09

09/13/2017, 10:15 AM
and also I'm getting it from the webservice
ok
how do you send intents to one kotlin activty to another and how do you read the?
g

gildor

09/13/2017, 10:16 AM
Exactly the same way as in java
s

sqia09

09/13/2017, 10:21 AM
in Kotlin? give me that line of code
and do you implement Parcelable the way I've tried to do?
g

gildor

09/13/2017, 10:22 AM
I do not implement Parcelable manually
Same way in kotlin, just copy paste
s

sqia09

09/13/2017, 10:23 AM
have you used @Parcelize?
g

gildor

09/13/2017, 10:23 AM
no
but it’s definitely nothing wrong to implement it manually
Are you sure that you need getParcelableArrayExtra? val siteListFromIntent = intent.getParcelableArrayExtra(“routesSiteList”)[0]
because looks like you send list instead of array
there is getParcelableArrayListExtra
2 Views