Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Serialize通过Intent传递自定义ArrayList时出现问题_Android_Arraylist - Fatal编程技术网

Android 使用Serialize通过Intent传递自定义ArrayList时出现问题

Android 使用Serialize通过Intent传递自定义ArrayList时出现问题,android,arraylist,Android,Arraylist,我试图将自定义对象的数组列表从一个类传递到另一个类。我使用包装器类传递数组列表: package perks; import java.io.Serializable; import java.util.ArrayList; public class PerkWrapper implements Serializable { private ArrayList<Perk> parliaments; public PerkWrapper(ArrayList<P

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

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

}
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);
}
其中player.perks是包含Perk对象的arrayList 我这样检索它:

PerkWrapper pw = (PerkWrapper) getIntent().getSerializableExtra("perks");
        plrPerks = pw.getParliaments();
        player.perks = plrPerks;
当我运行应用程序时,出现以下错误:

Unable to start activity.. ClassCastException ArrayList cannot be cast to   
perks.perkwrapper
这是我的Perk类:(数组列表中的对象):

套餐优惠;
公共类特权实现可包裹{
公共字符串名称;
公共字符串描述;
公共成本;
公共int roundReq;
兰克雷克公共酒店;
公营部门:;
公共int plusInc;
公共int自动点击;
公共奖励奖金;
公共津贴{
this.name=“”;
this.desc=“”;
这个成本=0;
this.roundReq=1;
这个。rankReq=1;
此参数为1;
这是1.plusInc=1;
此参数为0.autoClick=1;
这个。奖励奖金=1;
}
公共福利(包裹内){
name=in.readString();
desc=in.readString();
成本=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=新建Parcelable.Creator(){
公共特权createFromParcel(地块内){
返回新的额外津贴(in);
}
公共特权[]新数组(整数大小){
返回新的特权[大小];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地书面记录(名称);
目的地记录(说明);
目的写入(成本);
目的写入(roundReq);
目的地记录(rankReq);
目的地记录(12月底);
目的写入(plusInc);
目的写入(自动点击);
目的写入(奖励奖金);
}
}


如何防止发生此错误,或者除了简单地传递数组列表之外,还有其他方法吗?感谢您抽出时间

在活动之间传递arraylist,请使用-

Intent intent = new Intent(this, secondActivity.class);
        intent.putStringArrayListExtra("list", samplelist);
        startActivity(intent);
在接收意图中,您需要执行以下操作:

Intent i = getIntent();  
stock_list = i.getStringArrayListExtra("list");

您正在传递
player.perks
,这是
Perk
ArrayList
,在您的其他活动中,您将获得
PerkWrapper
,因此它会给您错误。您需要执行类似的操作

ArrayList<Perk> perks = (ArrayList<Perk>) getIntent().getSerializableExtra("perks");
ArrayList perks=(ArrayList)getIntent().getSerializableExtra(“perks”);
回复@amit singh的回答(我的声誉尚未达到评论水平 级别)


指定的方法只能用于格式为arraylist的arraylist。对于自定义对象,这是不适用的。

公共类PerkWrapper Extendes ArrayList implements Serializable
是player.perks PerkWrapper的实例吗?它起作用了。我还需要在Perk中实现parcelable吗?是的,Perk应该是
parcelable
。这只能用于传递字符串ArrayList而不是自定义ArrayList。为此,您必须使用parcelable或Serializable接口。我知道,我只是为其他人指出了它,要使用intent传递自定义arrayList,请参见以下问题该问题是关于传递自定义arrayList的
ArrayList<Perk> perks = (ArrayList<Perk>) getIntent().getSerializableExtra("perks");