Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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
Java intent.addExtra(可包裹对象)的NotSerializable异常_Java_Android_Android Intent_Arraylist_Parcel - Fatal编程技术网

Java intent.addExtra(可包裹对象)的NotSerializable异常

Java intent.addExtra(可包裹对象)的NotSerializable异常,java,android,android-intent,arraylist,parcel,Java,Android,Android Intent,Arraylist,Parcel,我正在尝试将包含其他对象的ArrayList的一个对象转移到Android中的另一个活动。这样做的目的是将它们添加到ArrayList中并显示它们,以便执行相关任务 Task ta = new Task(n, o, t, v, subList); Intent i = new Intent(this, ActivityList.class); i.putExtra("task", ta); startActivity(i); 这就是目的。任务是可分包的,子列表是类子任

我正在尝试将包含其他对象的ArrayList的一个对象转移到Android中的另一个活动。这样做的目的是将它们添加到ArrayList中并显示它们,以便执行相关任务

Task ta = new Task(n, o, t, v, subList);

    Intent i = new Intent(this, ActivityList.class);
    i.putExtra("task", ta);
    startActivity(i);
这就是目的。任务是可分包的,子列表是类子任务的ArrayList,也是可分包的。 当然,因为ArrayList总是实现可序列化,所以将它们打包应该不会有问题吧?构造函数参数是:String、byte、byte、byte、ArrayList。这些字节将用作布尔值

以下是任务的包裹代码(如果需要):

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(taskName);
    dest.writeByte((byte) (soundCueOne ? 1 : 0));
    dest.writeByte((byte) (soundCueTwo ? 1 : 0));
    dest.writeByte((byte) (vibrCue ? 1 : 0));
    dest.writeSerializable(myList);
}

private Task(Parcel in) {
    this.taskName = in.readString();
    this.soundCueOne = in.readByte() != 0;
    this.soundCueTwo = in.readByte() != 0;
    this.vibrCue = in.readByte() != 0;
    this.myList = (ArrayList<Subtask>) in.readSerializable();
}

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Task createFromParcel(Parcel in) {
        return new Task(in);
    }

    public Task[] newArray(int size) {
        return new Task[size];
    }
};
@覆盖
公共无效写入包裹(包裹目的地,内部标志){
目的写入限制(任务名称);
目标写入字节((字节)(soundCueOne?1:0));
dest.writeByte((字节)(soundCueTwo?1:0));
dest.writeByte((字节)(vibrCue?1:0));
目标可写变量(myList);
}
专用任务(包裹中){
this.taskName=in.readString();
this.soundCueOne=in.readByte()!=0;
this.soundCueTwo=in.readByte()!=0;
this.virbcue=in.readByte()!=0;
.readSerializable()中的this.myList=(ArrayList);
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共任务createFromParcel(中的地块){
返回新任务(在中);
}
公共任务[]新数组(整数大小){
返回新任务[大小];
}
};

有人能看出代码有什么问题吗?很明显,它在某个地方,甚至可能在子任务类中,因为仿真器在构建任务并试图打包它时就会崩溃

如果我看得没错,那么问题是您试图将
ArrayList
用作可序列化文件,即使它不是可序列化文件-它是可序列化文件

因此,更换

dest.writeSerializable(myList);

替换

this.myList = (ArrayList<Subtask>) in.readSerializable();
.readSerializable()中的this.myList=(ArrayList); 与

this.myList=in.readTypedList(新的ArrayList(),子任务.CREATOR);

子任务是否实现
可序列化
?@EpicPandaForce否,它实现可打包。尝试使用
子任务[]子任务=myList.toArray(新子任务[myList.size()]);dest.writeTypedArray(子任务、标志)
readTypedArray
但是根据你的经验,你也可以把这个列表写出来。@EpicPandaForce你认为你可以在代码中写出来吗?我似乎无法让它发挥作用,尽管我可能在这里做错了什么。。。
this.myList = (ArrayList<Subtask>) in.readSerializable();
this.myList = in.readTypedList(new ArrayList<Subtask>(), Subtask.CREATOR);