有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当我将一个对象作为一个包发送时,java试图打开它

我有一个复杂的类,我使用@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 (安卓.os.Parcelable) in Parcels cannot be applied to (java.lang.Object)"

共 (2) 个答案

  1. # 1 楼答案

    所以我只是在错误行按下alt enter键,它提供了正确的类型转换。。。我应该经常进去

  2. # 2 楼答案

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

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