views:

1413

answers:

2

I'm trying to write an array of objects that implement Parcelable into a Parcel using writeParcelableArray.

The objects I'm trying to write are defined (as you'd expect) as:

public class Arrival implements Parcelable {
    /* All the right stuff in here... this class compiles and acts fine. */
}

And I'm trying to write them into a `Parcel' with:

@Override
public void writeToParcel(Parcel dest, int flags) {
    Arrival[] a;
    /* some stuff to populate "a" */
    dest.writeParcelableArray(a, 0);
}

When Eclipse tries to compile this I get the error:

Bound mismatch: The generic method writeParcelableArray(T[], int) of type Parcel is not applicable for the arguments (Arrival[], int). The inferred type Arrival is not a valid substitute for the bounded parameter < T extends Parcelable >

I completely don't understand this error message. Parcelable is an interface (not a class) so you can't extend it. Anyone have any ideas?

UPDATE: I'm having basically the same problem when putting an ArrayList of Parcelables into an Intent:

Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);

yields:

The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)

This may be because Location was the class I was working on above (that wraps the Arrivals), but I don't think so.

+2  A: 

Actually, you can extend an interface, and it looks like you need to do just that. The generics parameter in writeParcelableArray is asking for an extended interface (not the interface itself). Try creating an interface MyParcelable extends Parcelable. Then declaring your array using the interface, but the impl should be your Arrival extends MyParcelable.

harschware
A: 

It turns out it just wanted me to build an array of Parcelables. To use the example from the question:

@Override
public void writeToParcel(Parcel dest, int flags) {
    Parcelable[] a;
    /* 
        some stuff to populate "a" with Arrival 
        objects (which implements Parcelable) 
    */
    dest.writeParcelableArray(a, 0);
}
fiXedd
As I said, declare your array using the interface. Glad you got it worked out.
harschware