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.
ArrayList<ParcelableObject> pointsExtra = new ArrayList<ParcelableObject>();
intent.putExtra("", pointsExtra);
http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra%28java.lang.String
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With