Issue
Reaching to ask for your help on this one.
I'm getting the error:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = java.util.ArrayList)
I'm already implementing Parcelable both in the Entities class (which is an ArrayList of Entity) as in the Entity class.
The code for the Entities class is as follows:
package my.package;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class Entities implements Parcelable {
private ArrayList<Entity> entities = new ArrayList<>();
public Entities() {
}
public ArrayList<Entity> getEntities() {
return entities;
}
public void setEntities(ArrayList<Entity> entities) {
this.entities = entities;
}
// Parcelable
private Entities(Parcel in) {
entities = (ArrayList<Entity>) in.readSerializable();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(entities);
}
public static final Parcelable.Creator<Entities> CREATOR = new Parcelable.Creator<Entities>() {
@Override
public Entities createFromParcel(Parcel parcel) {
return new Entities(parcel);
}
@Override
public Entities[] newArray(int i) {
return new Entities[i];
}
};
}
And to move into the next Activity I'm doing so:
ArrayList<Entity> entities = new ArrayList<Entity>();
Entities entities_ = new Entities();
for (int i = 0; i < jsonArray_Entities.length(); i++) {
JSONObject jsonObject_Entity = jsonArray_Entities.getJSONObject(i);
Entity entity = new Entity();
(Here goes all the entity.set() logic)
entities.add(entity);
}
entities_.setEntities(entities);
if (entities.size() > 0) {
Intent intent = new Intent(SearchEntitiesActivity.this, ListEntitiesActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("entities", entities_);
intent.putExtras(bundle);
startActivity(intent);
}
Can you identify what I'm doing wrong?
Thanks in advance.
Solution
For ArrayList
, I recommend writeParcelableList()
instead of writeSerializable()
, and readArrayList()
instead of readSerializable()
.
Of course, really I recommend Kotlin and @Parcelize
, where all this stuff gets nicely code-generated for you! 😀
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.