使用序列化通过 Intent 传递自定义 ArrayList 的问题 [英] Issue with passing a Custom ArrayList through Intent with Serialize

查看:20
本文介绍了使用序列化通过 Intent 传递自定义 ArrayList 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将自定义对象的数组列表从一个类传递到另一个类.我使用了一个包装类来传递数组列表:

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.putExtra("perks", player.perks); 

player.perks 是包含 Perk 对象的 arrayList我像这样检索它:

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

这是我的 Perk 类:(数组 List 中的对象):

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

推荐答案

您正在传递 player.perks 这是 PerkArrayList并且在您的其他活动中,您获得了 PerkWrapper,因此它给了您错误.您需要做这样的事情

You are passing player.perks which is an ArrayList of Perk and in your other activity you are getting PerkWrapper and hence it is giving you error.You will need to do something like this

ArrayList<Perk> perks = (ArrayList<Perk>) getIntent().getSerializableExtra("perks");

这篇关于使用序列化通过 Intent 传递自定义 ArrayList 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆