我正在尝试将一个对象作为 extra 放在一个 intent 上。该对象的类是我创建的,所以我将其设为 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());
}
}
在 Activity 中,我这样声明变量:
private List<NavigationDataSet> ds;
以及 Intent 创建:
public static Intent mapIntent(Context context){
Intent i = new Intent(context, mapsView.class);
i.putExtra("NavSet", ds);
return i;
}
变量 ds 在 onCreate 方法上执行的 AsyncTask 上初始化。 我在 putExtra 指令上遇到预编译错误:
Cannot make a static reference to the non-static field ds
但是在这里http://developer.android.com/reference/android/content/Intent.html它并没有说它必须是静态变量。
如果我将其更改为静态,它会显示:
The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List)
但我没有传递一个 boolean 值,它是一个 Parcelable!!!那我该怎么办?我真的不明白这是怎么回事。
最佳答案
ArrayList<ParcelableObject> pointsExtra = new ArrayList<ParcelableObject>();
intent.putExtra("", pointsExtra);
关于java - 不能在 Intent 上附加额外的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7958386/