android - 无法将Intent中的parcelable传递给另一个活动

时间:2017-02-25 19:51:07

标签: android android-activity parcelable

嘿,我有一个问题,将一个可分配的POJO传递给一个新的活动。我有一个listview列出了一堆新闻来源,它工作正常。当我点击一个项目时,我在意图中传递一个可分配的对象,并期望在“DetailsActivity”中,parcelable将在那里,但它的null,任何想法?在开始活动之前,我看到数据已成功投入到intent中。这是一些代码:

Result类实际上是我的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;
    }

它使用另一个名为Multimedium的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时,程序说以下是extra是null,但为什么呢?

    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 ?
//....
        }

1 个答案:

答案 0 :(得分:1)

我建议您使用此库:https://github.com/johncarl81/parceler