0

I have written the following classes:

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

And i got this exception whn i want to store object to onother fragment:

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)

I have read casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList but its does not help me. I will be glad to get any help

Error in this line

 queues =(ArrayList)  Arrays.asList(resultArray);
5
  • Queue is not Parcelable. Commented Oct 8, 2014 at 20:11
  • public static class Queue implements Parcelable Commented Oct 8, 2014 at 20:12
  • Is Queue one of your own classes then? Not java.util.Queue? Commented Oct 8, 2014 at 20:13
  • yes, look my code please Commented Oct 8, 2014 at 20:14
  • Ah, missed that bottom class. Commented Oct 8, 2014 at 20:15

1 Answer 1

3

You need to change the type of your field queue to List<Queue> rather than ArrayList<Queue>. You also need to change

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

to

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

The reason it's failing is that Arrays.asList() returns a List but not necessarily an ArrayList. You don't know the exact type that will be returned, so you should just cast to a List.

But List is a generic type, so you should include the type parameter, and cast it to List<Queue> rather than just List. (You'll get a compiler warning if you just use List.)

By the way, it's generally a good idea to avoid class names that are also used by standard JDK classes (like Queue). It will invite confusion for anyone who reads your code, and make it more likely that you'll accidentally use the wrong import at some point.

Sign up to request clarification or add additional context in comments.

3 Comments

Or alternately queues = new ArrayList<Queue>(Arrays.asList(resultArray));
@kcoppock Could do, but unless you really need it to be an ArrayList, this will just slow things down.
Agreed! :) Just an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.