Android Intent Parcelable:解组时找不到类

时间:2014-03-28 06:13:05

标签: android android-intent parcelable

我在Stackoverflow中查看了“解组时未找到类”的所有答案,但我找不到解决方案。我试过任何变种,但没有奏效。 我收到以下错误:

enter image description here

这是我的课程:

第一课:

public class Class1 implements Parcelable, Serializable {
    private static final long serialVersionUID = -3892107077759983950L;
    private long id;
    private ArrayList<Class2> details;
    public Class1(long id, ArrayList<Class2> details) {
        this.id = id;
        this.details = details;
    }
    public Class1(Parcel in) {
        this.id = in.readLong();
        details = new ArrayList<Class2>((Collection<? extends Class2>) Arrays.asList(in.readParcelableArray(Class2.class.getClassLoader())));
//      details = (ArrayList<OrderDetail>)in.readSerializable();
//      details = in.readArrayList(Class2.class.getClassLoader());
//      in.readList(details, Class2.class.getClassLoader());
//      in.readTypedList(details, Class2.CREATOR);
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
//      dest.writeSerializable(details);        
//      dest.writeList(details);
//      dest.writeTypedList(details);
        dest.writeParcelableArray(ordDetailListToArr(), flags);
    }
    @Override
    public int describeContents() { return 0; }
    public static final Parcelable.Creator<Class1> CREATOR
    = new Parcelable.Creator<Class1>() {
        public Class1 createFromParcel(Parcel in) {
            return new Class1(in);
        }
        public Class1[] newArray(int size) {
            return new Class1[size];
        }
    };
}

第二类:

public class Class2 implements Parcelable, Serializable {
    private static final long serialVersionUID = 3242734374178052427L;
    private long orderId;
    private Class3 Class3;
    public Class2(long orderId, Class3 Class3) {
        this.orderId = orderId;
        this.Class3 = Class3;
    }
    private Class2(Parcel in) {
        this.orderId = in.readLong();
        this.Class3 = (Class3) in.readParcelable(Class3.class.getClassLoader());
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(orderId);
        dest.writeParcelable(Class3, flags);
    }
    @Override
    public int describeContents() { return 0;}
    public static final Parcelable.Creator<Class2> CREATOR
    = new Parcelable.Creator<Class2>() {
        public Class2 createFromParcel(Parcel in) {
            return new Class2(in);
        }
        public Class2[] newArray(int size) {
            return new Class2[size];
        }
    };
}

第三类:

public class Class3 extends Class4 implements Cloneable, Serializable {
    private static final long serialVersionUID = 8712386538880552241L;
    private int count;
    public Class3(Class4 Class4, int count) {
        super(Class4.getId(), Class4.getName());
        this.count = count;
    }
    private Class3(Parcel in) {
        super(in);
        this.count = in.readInt();
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeInt(count);
    }
    @Override
    public int describeContents() { return 0;}
    public static final Parcelable.Creator<Class3> CREATOR
    = new Parcelable.Creator<Class3>() {
        public Class3 createFromParcel(Parcel in) {
            return new Class3(in);
        }
        public Class3[] newArray(int size) {
            return new Class3[size];
        }
    };
}

第四课:

public class Class4 implements Parcelable, Serializable {
    private static final long serialVersionUID = -8394588748958519736L;
    private long id;
    private String name;
        public Class4(long id, String name) {
        this.id = id;
        this.name = name;
    }
    public Class4(Parcel in) {
        this.id = in.readLong();
        this.name = in.readString();
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(name);
    }
    @Override
    public int describeContents() { return 0;}
    public static final Parcelable.Creator<Class4> CREATOR
    = new Parcelable.Creator<Class4>() {
        public Class4 createFromParcel(Parcel in) {
            return new Class4(in);
        }
        public Class4[] newArray(int size) {
            return new Class4[size];
        }
    };  
}

我在所有类中使用Serializable通过Socket通道交换(读/写对象)数据。

我将第一个活动的数据传递给第二个活动:

第一项活动:

Class1 class1 = ...;
Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
orderedProductsActivity.putExtra("mydata", (Parcelable)class1);
startActivity(secondActivity);

第二项活动:

Class1 class1 = (Class1) getIntent().getParcelableExtra("mydata");

1 个答案:

答案 0 :(得分:0)

我以前遇到过这个问题,在我的情况下它与ArrayList有关,你可以尝试这样做:

array = (ArrayList<Long>) in.readSerializable();

dest.writeSerializable(array);

而不是使用writeParcelableArray