我想把一個對象作爲額外的意圖。該對象的類是由我創建的,所以我將其設爲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());
}
}
活動中,我聲明如下變量:
private List<NavigationDataSet> ds;
,並且打算創建:
public static Intent mapIntent(Context context){
Intent i = new Intent(context, mapsView.class);
i.putExtra("NavSet", ds);
return i;
}
變量DS是在其上執行的的AsyncTask初始化onCreate方法。 而我上了putExtra指令預編譯錯誤:
不能使靜態參考非靜態場DS
但這裏http://developer.android.com/reference/android/content/Intent.html不說它是一個靜態的變量。
如果我將其更改爲靜態的,它說:
在型意圖的方法putExtra(字符串,布爾)不 適用於參數(字符串列表)
但我沒有通過一個布爾值,它是一個Parcelable!那麼我該怎麼做?我真的不明白這個工作的方式。
工作!我以爲我曾嘗試過,但似乎我沒有。謝謝! – ferguior