【问题标题】:android - unable to pass parcelable in Intent to another activityandroid - 无法将 Intent 中的 parcelable 传递给另一个活动
【发布时间】:2017-02-25 19:51:07
【问题描述】:

嘿,我在将可打包 POJO 传递给新活动时遇到问题。我有一个列表视图,其中列出了一堆新闻来源,它工作正常。当我单击一个项目时,我在意图中传递了一个可打包对象,并在“DetailsActivity”中期望该可打包对象将在那里,但它为空,有什么想法吗?在开始活动之前,我看到数据已成功放入意图中。这是一些代码:

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 的可打包类:

 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 实现中,或者问题是否涵盖所有额外内容。
  • 传递字符串效果很好,刚刚尝试过。日志中没有任何内容。活动在清单中定义如下:
  • 好吧,如果字符串有效,则表明自定义 Parcleable 存在问题,但如果是这种情况,我预计会发生崩溃。 Using custom Parcelables is dicey,但是在你自己的两个活动之间传递应该是安全的。尝试使用this codeParcelable 转换为byte[],看看是否崩溃等

标签: android android-activity parcelable


【解决方案1】:

我推荐你使用这个库:https://github.com/johncarl81/parceler

【讨论】:

  • 一些如何使用这个库解决了这个问题。我正在从jsonschema2pojo.org 生成包裹,但不确定它有什么不同,但谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2014-03-07
相关资源
最近更新 更多