java.util.Arrays $ ArrayList无法强制转换为Parcelable对象中的java.util.ArrayList

时间:2014-10-08 20:08:27

标签: android arrays list arraylist parcelable

我写过以下课程:

public class Company implements Parcelable {

private int uid;
@SerializedName("field_user_org_name_value")
private String name;
@SerializedName("field_user_org_address_value")
private String address;
@SerializedName("field_user_org_category_tid")
private int category;
@SerializedName("field_user_org_town_tid")
private int cityId;
@SerializedName("image")
private String imageUrl;
private int online;
@SerializedName("field_user_org_description_value")
private String description;
@SerializedName("field_user_org_phone_value")
private String mobPhone;
@SerializedName("field_user_org_stac_phone_value")
private String phone;

public ArrayList<Queue> getQueues() {
    return queues;
}

@SerializedName("value_array")
private ArrayList<Queue> queues;

double lat;
double lng = 0;
double distance;


public double getDistance() {
    return distance;
}

public void setDistance(double distance) {
    this.distance = distance;
}

public double getLng() {
    return lng;
}

public void setLng(double lng) {
    this.lng = lng;
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public String getDescription() {
    return description;
}

public String getMobPhone() {
    return mobPhone;
}

public String getPhone() {
    return phone;
}

public int getOnline() {
    return online;
}

public Company(Parcel in) {
    uid = in.readInt();
    name = in.readString();
    address = in.readString();
    category = in.readInt();
    cityId = in.readInt();
    imageUrl = in.readString();
    online = in.readInt();
    description = in.readString();
    mobPhone = in.readString();
    phone = in.readString();
    lat = in.readDouble();
    lng = in.readDouble();
    distance = in.readDouble();
    Parcelable[] parcelableArray = in.readParcelableArray(Queues.class.getClassLoader());
    Queue[] resultArray = null;
    if (parcelableArray != null) {
        resultArray = Arrays.copyOf(parcelableArray, parcelableArray.length, Queue[].class);
    }
    queues =(ArrayList)  Arrays.asList(resultArray);
}

public int getUid() {
    return uid;
}

public String getName() {
    return name;
}

public String getAddress() {
    return address;
}

public int getCategory() {
    return category;
}

public int getCityId() {
    return cityId;
}

public String getImageUrl() {
    return imageUrl;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(uid);
    dest.writeString(name);
    dest.writeString(address);
    dest.writeInt(category);
    dest.writeInt(cityId);
    dest.writeString(imageUrl);
    dest.writeInt(online);
    dest.writeString(description);
    dest.writeString(mobPhone);
    dest.writeString(phone);
    dest.writeDouble(lat);
    dest.writeDouble(lng);
    dest.writeDouble(distance);
    Queue[] queueArray = new Queue[queues.size()];
    dest.writeParcelableArray(queues.toArray(queueArray), 0);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

    @Override
    public Object createFromParcel(Parcel source) {
        return new Company(source);
    }

    @Override
    public Object[] newArray(int size) {
        return new Company[size];
    }
};


public static class CustomComparator implements Comparator<Company> {
    @Override
    public int compare(Company o1, Company o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

public static class CustomComparatorDistance implements Comparator<Company> {
    @Override
    public int compare(Company o1, Company o2) {
        return Double.compare(o1.getDistance(), o2.getDistance());
    }
}

public static class Queue implements Parcelable {
    public int getCondition() {
        return condition;
    }

    public String getTime() {
        return time;
    }

    /**
     * 0 - Free
     * 1 - Busy
     * 2 - Not working
     */
    @SerializedName("sel")
    int condition;
    String time;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(condition);
        dest.writeString(time);
    }

    public Queue(Parcel in) {

        condition = in.readInt();
        time = in.readString();
    }

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

        public Object[] newArray(int size) {
            return new Queue[size];
        }
    };
}
}

我得到了这个例外,我想将对象存储到另一个片段:

java.lang.RuntimeException: Unable to start activity ComponentInfo{khuta.freeturn/khuta.freeturn.fragments.CompanyInfoActivity}: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
        at khuta.freeturn.models.Company.<init>(Company.java:106)
        at khuta.freeturn.models.Company$1.createFromParcel(Company.java:161)
        at android.os.Parcel.readParcelable(Parcel.java:2104)
        at android.os.Parcel.readValue(Parcel.java:2013)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:2314)
        at android.os.Bundle.unparcel(Bundle.java:249)
        at android.os.Bundle.getParcelable(Bundle.java:1206)
        at android.content.Intent.getParcelableExtra(Intent.java:4652)
        at khuta.freeturn.fragments.CompanyInfoActivity.onCreate(CompanyInfoActivity.java:77)
        at android.app.Activity.performCreate(Activity.java:5231)

我已阅读casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList,但它对我没有帮助。我很乐意得到任何帮助

此行中的错误

 queues =(ArrayList)  Arrays.asList(resultArray);

1 个答案:

答案 0 :(得分:2)

您需要将字段queue的类型更改为List<Queue>而不是ArrayList<Queue>。您还需要更改

queues = (ArrayList)  Arrays.asList(resultArray);

queues = (List<Queue>)  Arrays.asList(resultArray);

它失败的原因是Arrays.asList()返回List但不一定是ArrayList。您不知道将返回的确切类型,因此您应该转换为List

但是List是一种泛型类型,因此您应该包含type参数,并将其转换为List<Queue>而不是List。 (如果您只使用List,则会收到编译器警告。)

顺便说一下,避免使用标准JDK类(如Queue)也使用的类名通常是一个好主意。它会让任何阅读您代码的人感到困惑,并且更有可能在某些时候意外地使用了错误的import