I am attempting to pass an array List of Custom objects through from one class to the other. I used a wrapper class to pass over the array List:
package perks;
import java.io.Serializable;
import java.util.ArrayList;
public class PerkWrapper implements Serializable {
private ArrayList<Perk> parliaments;
public PerkWrapper(ArrayList<Perk> perks) {
this.parliaments = perks;
}
public ArrayList<Perk> getParliaments() {
return this.parliaments;
}
}
I pass it like this:
i.putExtra("perks", player.perks);
Where player.perks is the arrayList containing teh Perk object And i retrieve it like so:
PerkWrapper pw = (PerkWrapper) getIntent().getSerializableExtra("perks");
plrPerks = pw.getParliaments();
player.perks = plrPerks;
When i run the app, i get the following error:
Unable to start activity.. ClassCastException ArrayList cannot be cast to
perks.perkwrapper
Here is my Perk class: (The object in the array List):
package perks;
public class Perk implements Parcelable {
public String name;
public String desc;
public int cost;
public int roundReq;
public int rankReq;
public int minusDec;
public int plusInc;
public int autoClick;
public int rewardBonus;
public Perk() {
this.name = "";
this.desc = "";
this.cost = 0;
this.roundReq = 1;
this.rankReq = 1;
this.minusDec = 1;
this.plusInc = 1;
this.autoClick = 1;
this.rewardBonus = 1;
}
public Perk(Parcel in) {
name = in.readString();
desc = in.readString();
cost = in.readInt();
roundReq = in.readInt();
rankReq = in.readInt();
minusDec = in.readInt();
plusInc = in.readInt();
autoClick = in.readInt();
rewardBonus = in.readInt();
}
public static final Parcelable.Creator<Perk> CREATOR = new Parcelable.Creator<Perk>() {
public Perk createFromParcel(Parcel in) {
return new Perk(in);
}
public Perk[] newArray(int size) {
return new Perk[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(desc);
dest.writeInt(cost);
dest.writeInt(roundReq);
dest.writeInt(rankReq);
dest.writeInt(minusDec);
dest.writeInt(plusInc);
dest.writeInt(autoClick);
dest.writeInt(rewardBonus);
}
}
How can i prevent this error from happening or are there any alternatives to passing array Lists simply? Thank you for your time