不能在意图上添加额外内容
我试图将一个对象作为额外的意图。该对象的类是我创建的,所以我将其设为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;
以及意图创建:
public static Intent mapIntent(Context context){
Intent i = new Intent(context, mapsView.class);
i.putExtra("NavSet", ds);
return i;
}
变量 ds 在 onCreate 方法上执行的 AsyncTask 上初始化。 我在 putExtra 指令上遇到了预编译错误:
无法对非静态字段 ds 进行静态引用
但是这里 http://developer .android.com/reference/android/content/Intent.html 它并没有说它必须是静态变量。
如果我将其更改为静态,它会显示:
Intent 类型中的方法 putExtra(String, boolean) 不是 适用于参数(字符串、列表)
但我没有传递布尔值,它是一个 Parcelable!那我该怎么办?我真的不明白这是如何运作的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://developer.android.com/reference /android/content/Intent.html#putParcelableArrayListExtra%28java.lang.String
http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra%28java.lang.String
在您的情况下, ds 不是静态变量,因此您不能在静态方法中引用它。要么使 ds 静态,将 is 作为参数传递给您的 mapIntent 函数,要么使您的 mapIntent 函数不是静态的。
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.