以期通过自定义的ArrayList通过与意向序列化 [英] Issue with passing a Custom ArrayList through Intent with Serialize

查看:170
本文介绍了以期通过自定义的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是包含德帕金斯对象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

下面是我的咖啡馆类:(数组列表中的对象):

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);
}

}

我怎么能prevent这个错误的发生或者有任何替代传递数组列表简单?谢谢你的时间

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 这是一个的ArrayList 咖啡馆,并在你的其他活动,你也越来越 PerkWrapper ,因此它是给你error.You会需要做这样的事情。

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");

这篇关于以期通过自定义的ArrayList通过与意向序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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