【问题标题】:Trying to unwrap an object when I send it as a bundle当我将对象作为捆绑发送时尝试解开它
【发布时间】:2015-04-24 02:26:53
【问题描述】:

我有一个复杂的类,我使用了@Parcelable(我正在使用 Parceler 库 https://github.com/johncarl81/parceler

@Parcel
public class Event {
    public int id;
    public String title;
    public String description;
    public String resourceURL;
    public List<Url> urls;
    public Date modified;
    public Date start;
    public Date end;
    public ImageInfo thumbnail;
    public ItemList comics;
    public ItemList stories;
    public ItemList series;
    public CharacterList characters;
    public CreatorList creators;
    public Item next;
    public Item previous;
}

该类还有许多其他对象,但它们大多是字符串。

我将它包装在我的主要活动中,例如:

Intent intent = new Intent(getApplicationContext(), EventDescriptionActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("event", Parcels.wrap(eventList.get(position)));
intent.putExtras(bundle);
startActivity(intent);

我有一个事件列表 (eventList),我试图将列表中的一个对象发送到另一个活动。

我在我的其他活动中打开它,例如:

Event event = Parcels.unwrap(this.getIntent().getExtras().get("event"));

在我的 onCreate() 中

但是我的参数下面有一条红线:

"unwrap (android.os.Parcelable) in Parcels cannot be applied to (java.lang.Object)"

【问题讨论】:

    标签: java android parcelable parceler


    【解决方案1】:

    所以我只是在错误行上按了 alt enter,它提供了正确的类型转换...我应该更频繁地输入 alt

    【讨论】:

      【解决方案2】:

      这最初是文档中的一个错误。与Parcels.unwrap 方法结合使用的额外方法是getParcelableExtra 方法:

      Event event = Parcels.unwrap(getIntent().getParcelableExtra("event"));
      

      【讨论】:

        猜你喜欢
        • 2020-12-27
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 2018-05-06
        • 2015-05-29
        • 2021-09-01
        • 1970-01-01
        相关资源
        最近更新 更多