2011-10-31 90 views
3

我想把一个对象作为额外的意图。该对象的类是由我创建的,所以我将其设为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!那么我该怎么做?我真的不明白这个工作的方式。

回答

3

在你的情况下,ds不是一个静态变量,因此你不能在静态方法中引用它。要么使ds静态,将pass作为您的mapIntent函数的参数,要么使mapIntent函数不是静态的。

+0

如果我让静态的,因为我在后说,它告诉我,“在型意图的方法putExtra(字符串,布尔)不适用的参数(字符串, List )“,这是愚蠢的,因为我打算使用putExtra(String,Parcelable) – ferguior

+1

'List '不等于'Parcelable'。 'NavigationDataSet'是,但不是'List'。 – Peterdk

+0

它不能把它作为putExtra(String,Parcelable [])或putExtra(String,List)吗?那么我应该怎么传递它呢? – ferguior