嘿,我有一個問題傳遞一個parcelable POJO到一個新的活動。我有一個listview列出了一堆新聞來源,它工作正常。當我點擊一個項目時,我傳遞了一個parcelable對象的意圖,並期望在「DetailsActivity」中,parcelable會在那裏,但它的null,任何想法?在我開始活動之前,我看到數據已成功投入了意圖。下面是一些代碼:android-無法在意圖傳遞parcelable到另一個活動
結果類實際上是我的parcelable POJO,看起來像這樣:
public class Result implements Parcelable
{
@SerializedName("section")
@Expose
private String section;
@SerializedName("subsection")
@Expose
private String subsection;
@SerializedName("title")
@Expose
private String title;
@SerializedName("abstract")
@Expose
private String _abstract;
@SerializedName("url")
@Expose
private String url;
@SerializedName("byline")
@Expose
private String byline;
@SerializedName("item_type")
@Expose
private String itemType;
@SerializedName("updated_date")
@Expose
private String updatedDate;
@SerializedName("created_date")
@Expose
private String createdDate;
@SerializedName("published_date")
@Expose
private String publishedDate;
@SerializedName("material_type_facet")
@Expose
private String materialTypeFacet;
@SerializedName("kicker")
@Expose
private String kicker;
@SerializedName("des_facet")
@Expose(serialize = false, deserialize = false)
private List<String> desFacet = null;
@SerializedName("org_facet")
@Expose(serialize = false, deserialize = false)
private String orgFacet;
@SerializedName("per_facet")
@Expose(serialize = false, deserialize = false)
private String perFacet;
@SerializedName("geo_facet")
@Expose(serialize = false, deserialize = false)
private List<String> geoFacet = null;
@SerializedName("multimedia")
@Expose
private List<Multimedium> multimedia = null;
public final static Parcelable.Creator<Result> CREATOR = new Creator<Result>() {
@SuppressWarnings({
"unchecked"
})
public Result createFromParcel(Parcel in) {
Result instance = new Result();
instance.section = ((String) in.readValue((String.class.getClassLoader())));
instance.subsection = ((String) in.readValue((String.class.getClassLoader())));
instance.title = ((String) in.readValue((String.class.getClassLoader())));
instance._abstract = ((String) in.readValue((String.class.getClassLoader())));
instance.url = ((String) in.readValue((String.class.getClassLoader())));
instance.byline = ((String) in.readValue((String.class.getClassLoader())));
instance.itemType = ((String) in.readValue((String.class.getClassLoader())));
instance.updatedDate = ((String) in.readValue((String.class.getClassLoader())));
instance.createdDate = ((String) in.readValue((String.class.getClassLoader())));
instance.publishedDate = ((String) in.readValue((String.class.getClassLoader())));
instance.materialTypeFacet = ((String) in.readValue((String.class.getClassLoader())));
instance.kicker = ((String) in.readValue((String.class.getClassLoader())));
in.readList(instance.desFacet, (java.lang.String.class.getClassLoader()));
instance.orgFacet = ((String) in.readValue((String.class.getClassLoader())));
instance.perFacet = ((String) in.readValue((String.class.getClassLoader())));
in.readList(instance.geoFacet, (java.lang.String.class.getClassLoader()));
in.readList(instance.multimedia, (Multimedium.class.getClassLoader()));
return instance;
}
public Result[] newArray(int size) {
return (new Result[size]);
}
}
;
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getSubsection() {
return subsection;
}
public void setSubsection(String subsection) {
this.subsection = subsection;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAbstract() {
return _abstract;
}
public void setAbstract(String _abstract) {
this._abstract = _abstract;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getByline() {
return byline;
}
public void setByline(String byline) {
this.byline = byline;
}
public String getItemType() {
return itemType;
}
public void setItemType(String itemType) {
this.itemType = itemType;
}
public String getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getPublishedDate() {
return publishedDate;
}
public void setPublishedDate(String publishedDate) {
this.publishedDate = publishedDate;
}
public String getMaterialTypeFacet() {
return materialTypeFacet;
}
public void setMaterialTypeFacet(String materialTypeFacet) {
this.materialTypeFacet = materialTypeFacet;
}
public String getKicker() {
return kicker;
}
public void setKicker(String kicker) {
this.kicker = kicker;
}
public List<String> getDesFacet() {
return desFacet;
}
public void setDesFacet(List<String> desFacet) {
this.desFacet = desFacet;
}
public String getOrgFacet() {
return orgFacet;
}
public void setOrgFacet(String orgFacet) {
this.orgFacet = orgFacet;
}
public String getPerFacet() {
return perFacet;
}
public void setPerFacet(String perFacet) {
this.perFacet = perFacet;
}
public List<String> getGeoFacet() {
return geoFacet;
}
public void setGeoFacet(List<String> geoFacet) {
this.geoFacet = geoFacet;
}
public List<Multimedium> getMultimedia() {
return multimedia;
}
public void setMultimedia(List<Multimedium> multimedia) {
this.multimedia = multimedia;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(section);
dest.writeValue(subsection);
dest.writeValue(title);
dest.writeValue(_abstract);
dest.writeValue(url);
dest.writeValue(byline);
dest.writeValue(itemType);
dest.writeValue(updatedDate);
dest.writeValue(createdDate);
dest.writeValue(publishedDate);
dest.writeValue(materialTypeFacet);
dest.writeValue(kicker);
dest.writeList(desFacet);
dest.writeValue(orgFacet);
dest.writeValue(perFacet);
dest.writeList(geoFacet);
dest.writeList(multimedia);
}
public int describeContents() {
return 0;
}
,並使用另一個被稱爲多介質parcelable類:在
public class Multimedium implements Parcelable
{
@SerializedName("url")
@Expose
private String url;
@SerializedName("format")
@Expose
private String format;
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("width")
@Expose
private Integer width;
@SerializedName("type")
@Expose
private String type;
@SerializedName("subtype")
@Expose
private String subtype;
@SerializedName("caption")
@Expose
private String caption;
@SerializedName("copyright")
@Expose
private String copyright;
public final static Parcelable.Creator<Multimedium> CREATOR = new Creator<Multimedium>() {
@SuppressWarnings({
"unchecked"
})
public Multimedium createFromParcel(Parcel in) {
Multimedium instance = new Multimedium();
instance.url = ((String) in.readValue((String.class.getClassLoader())));
instance.format = ((String) in.readValue((String.class.getClassLoader())));
instance.height = ((Integer) in.readValue((Integer.class.getClassLoader())));
instance.width = ((Integer) in.readValue((Integer.class.getClassLoader())));
instance.type = ((String) in.readValue((String.class.getClassLoader())));
instance.subtype = ((String) in.readValue((String.class.getClassLoader())));
instance.caption = ((String) in.readValue((String.class.getClassLoader())));
instance.copyright = ((String) in.readValue((String.class.getClassLoader())));
return instance;
}
public Multimedium[] newArray(int size) {
return (new Multimedium[size]);
}
}
;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSubtype() {
return subtype;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(url);
dest.writeValue(format);
dest.writeValue(height);
dest.writeValue(width);
dest.writeValue(type);
dest.writeValue(subtype);
dest.writeValue(caption);
dest.writeValue(copyright);
}
public int describeContents() {
return 0;
}
}
所以我Mainactivity我有以下方法在調試器中看起來很好:
@Override
public void goToDetailsActivity(Result data) {
Intent intent = new Intent(MainActivity.this, DetailViewActivity.class);
intent.putExtra("newsInfo", data);
startActivity(intent);
}
然後當我去DetailViewActivity該程序說以下是多餘的爲空,但爲什麼?
public class DetailViewActivity extends Activity {
private String storyURL = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
extras.getParcelable("newsInfo") //this is null , why ?
getIntent().hasExtra("newsInfo") //this is also false ? why ?
//....
}
尋找在logcat中與堆棧跟蹤警告。另外,你確定沒有'DetailViewActivity'的現有實例,可能沒有這個額外的,已經在內存中並且可能被重用(例如,通過清單中的android:launchMode?)。你也可以臨時切換到一個簡單的額外(例如,一些布爾值),看看是否能夠結束,如果問題出現在你的'Parcelable'實現中,或者問題覆蓋了所有額外特性,那麼可以幫助確定它是否結束。 – CommonsWare
傳遞一個字符串很好,只是試了一下。並且日誌中沒有任何內容。這個活動在清單中是這樣定義的: –
j2emanue
好吧,如果一個字符串有效,這表明自定義的Parcleable存在問題,但我期望碰到一個崩潰如果是這樣的話。 [使用自定義'Parcelables'很冒險](https://commonsware.com/blog/2016/07/22/be-careful-where-you-use-custom-parcelables.html),但是在你自己的兩個之間傳遞活動應該是安全的。嘗試使用[此代碼](http://stackoverflow.com/questions/18000093/how-to-marshall-and-unmarshall-a-parcelable-to-a-byte-array-with-help-of-parcel/18000094 #18000094)將'Parcelable'轉換爲'byte []',看看你是否崩潰等。 – CommonsWare