android - Kotlin getParcelableArray from intent bundle not able to cast it to custom type -


i running weird problem . have class implements parcelable interface in kotlin.

i passing array of class 1 activity no issues here.

 var arrayofa:array<a> // tell type assume array initialised value  intent.putextra("array", arrayofa) 

but while receiving in activity , not able assign variable of type array asking me assign array when on type parcelable why not able assign variable.

in second activity  var arrayofa:array<a>?=null   arraya=intent.get("array") array<a> // problem here. class cast exception 

i not able understand why. can 1 me here. don't want change type of variable array as many inter depedencies.(the class here demonstration)

========================================

class a(val a:string?,val b:string?):parcelable {     constructor(parcel: parcel) : this(             parcel.readstring(),             parcel.readstring()) {     }      override fun writetoparcel(parcel: parcel, flags: int) {         parcel.writestring(a)         parcel.writestring(b)     }      override fun describecontents(): int {         return 0     }      companion object creator : parcelable.creator<a> {         override fun createfromparcel(parcel: parcel): {             return a(parcel)         }          override fun newarray(size: int): array<a?> {             return arrayofnulls(size)         }     } } 

you need use getparcelablearrayextra retrieve parcelable objects in

arraya = intent.getparcelablearrayextra("array") array<a?> 

make class a nullable over, because can't cast nullable non nullable in kotlin

creator should below

companion object creator : parcelable.creator<a?> {         override fun createfromparcel(parcel: parcel): a? {             return a(parcel)         }          override fun newarray(size: int): array<a?> {             return arrayofnulls(size)         }     } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -