ferguior
ferguior

Reputation: 271

Cannot put extra on an intent

I'm trying to put an object as extra on an intent. The class of the object was created by me, so I made it Parcelable.

public class NavigationDataSet implements Parcelable {

    private ArrayList<Placemark> placemarks = new ArrayList<Placemark>();
    private Placemark currentPlacemark;
    private Placemark routePlacemark;

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        // TODO Auto-generated method stub
        out.writeList(placemarks);
        out.writeValue(currentPlacemark);
        out.writeValue(routePlacemark);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<NavigationDataSet> CREATOR = new Parcelable.Creator<NavigationDataSet>() {
        public NavigationDataSet createFromParcel(Parcel in) {
            return new NavigationDataSet(in);
        }

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

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private NavigationDataSet(Parcel in) {
        in.readTypedList(placemarks, Placemark.CREATOR);
        this.currentPlacemark = in.readParcelable((ClassLoader) Placemark.CREATOR);
        this.routePlacemark = in.readParcelable(Placemark.class.getClassLoader());
    }
}

In the Activity, I declared the variable like this:

private List<NavigationDataSet> ds;

And the intent creation:

public static Intent mapIntent(Context context){
        Intent i = new Intent(context, mapsView.class);
        i.putExtra("NavSet", ds);
        return i;
}

The variable ds is initialized on an AsyncTask that is executed on the onCreate method. And I got a precompiling error on the putExtra instruction:

Cannot make a static reference to the non-static field ds

But here http://developer.android.com/reference/android/content/Intent.html it doesn't say it has to be a static variable.

And if I change it to static, the it says:

The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List)

But I'm not passing a boolean, it's a Parcelable!!! So what do I do? I really don't understand the way this is working.

Upvotes: 3

Views: 5678

Answers (2)

user634545
user634545

Reputation: 9429

ArrayList<ParcelableObject> pointsExtra = new ArrayList<ParcelableObject>();
intent.putExtra("", pointsExtra);

http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra%28java.lang.String

Upvotes: 5

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

In your case, ds is not a static variable, therefore you can't reference it in a static method. Either make ds static, pass is as an argument to your mapIntent function, or make your mapIntent function not static.

Upvotes: 3

Related Questions