2017-07-26 63 views
0

我把意圖我用下面的代碼對象(而把名單確實存在,我在調試器中選中)將序列化爲意圖不工作

Intent intent = new Intent(getApplicationContext(), ProductActivity.class); 
intent.putExtra("product", CategoryActivity.this.list.get(position)); 
        startActivity(intent); 

然後retrive

Intent intent = getIntent(); 
product = (Product) intent.getSerializableExtra("product"); 

但它是空

Model類是繼

public class Product implements Serializable { 

    Integer pk; 
    String name; 
    String description; 
    Integer price; 
    String image_url; 
    List<Option> options; 

    public Product(Integer pk, String name, String description, Integer price, String image_url, List<Option> options) { 
     this.pk = pk; 
     this.name = name; 
     this.description = description; 
     this.price = price; 
     this.image_url = image_url; 
     this.options = options; 
    } 

    public Integer getPk() { 
     return pk; 
    } 

    public void setPk(Integer pk) { 
     this.pk = pk; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public Integer getPrice() { 
     return price; 
    } 

    public void setPrice(Integer price) { 
     this.price = price; 
    } 

    public String getImage_url() { 
     return image_url; 
    } 

    public void setImage_url(String image_url) { 
     this.image_url = image_url; 
    } 

    public List<Option> getOptions() { 
     return options; 
    } 

    public void setOptions(List<Option> options) { 
     this.options = options; 
    } 
} 

我在做什麼錯?

+2

你也檢查list.get(位置)沒有返回null? – lelloman

+1

您是否正在使用'startActivity(intent)'重新啓動已經啓動的活動? –

回答

0

您必須添加更多代碼才能在意圖數據中使用您的類。

 import android.os.Parcel; 
     import android.os.Parcelable; 

     public class Yourclass implements Parcelable { 
     //your code 

     @Override 
     public void writeToParcel(Parcel out, int flags) { 
     //e.g. 
      out.writeInt(this.getWhat()); 
     //similar code 
     } 

     @Override 
     public int describeContents() { 
      return 0; 
     } 


    private YourClass(Parcel in) { 
    //e.g. 
     this.setWhat(in.readInt()); 
     } 

public static final Parcelable.Creator<YourClass> CREATOR 
= new Parcelable.Creator<YourClass>() { 

public YourClass createFromParcel(Parcel in) { 
     return new YourClass(in); 
    } 

public YourClass[] newArray(int size) { 
     return new YourClass[size]; 
    } 
}; 
     } 

存儲使用intent.putExtra(EXTRANAME, obj);檢索使用YourClass myObj = (YourClass) getIntent().getParcelableExtra(EXTRANAME);

+0

你的意思是使用Parcelable而不是Serialializable不僅僅是最佳實踐,而是Serializable實際上並不工作? – lelloman

+0

@lelloman,我發現這樣的解決方案可以用於意圖 – Vyacheslav

+0

內部的數據傳輸,它的工作原理是在Android上使用Parcelable而不是Serializable。但是,我認爲Serializable應該可以正常工作,Intent具有'putExtra(String,Serializable)'和'getSerializableExtra(String)'方法 – lelloman