【问题标题】:Unable to find generated Parcelable class with Realm无法使用 Realm 找到生成的 Parcelable 类
【发布时间】:2018-03-04 23:11:45
【问题描述】:

我正在尝试使用捆绑包传递领域对象,我使用了 Parcel

这是我的领域模型类。 专辑.java

@Parcel
public class Album extends RealmObject implements Serializable {
@PrimaryKey
  public String id;
  public String upc;
  public String albumName;
  public String albumArtUrl;
  public String artistName;
  public String genre_id;
  public String genreName;
  public String price;
  public String releaseYear;
  public int explicit;
  public RealmList<Song> songs = new RealmList<>();
}

这是 Song.java。

@Parcel
public class Song extends RealmObject implements Serializable {
  @PrimaryKey
  public String id;
  public String isrc;
  public String songName;
  public String artistName;
  public String album_id;
  public String albumArtUrl;
  public String genre_id;
  public String genreName;
  public String releaseYear;
  public String price;
  public String lyrics;
  public String demo;
  public int explicit;
}

当我尝试像这样在包中传递专辑对象时,

b.putParcelable("album", Parcels.wrap(album));

我遇到了这个错误。

Unable to find generated Parcelable class for com.devhousemyanmar.juketrill.models.Album, verify that your class is configured properly and that the Parcelable class com.devhousemyanmar.juketrill.models.Album$$Parcelable is generated by Parceler.

请帮我解决这个问题。

【问题讨论】:

标签: java android realm parcel


【解决方案1】:

如果您查看文档,它有一个专门用于 using Parceler 的部分。

// All classes that extend RealmObject will have a matching RealmProxy class created
// by the annotation processor. Parceler must be made aware of this class. Note that
// the class is not available until the project has been compiled at least once.
@Parcel(implementations = { PersonRealmProxy.class },
        value = Parcel.Serialization.BEAN, // <-- requires getters/setters if set
        analyze = { Person.class })
public class Person extends RealmObject {
    // ...
}

但值得注意的是,如果使用realm.copyFromRealm(song),则无需指定implementations = {PersonRealmProxy.class},然后将其传递给Parcels.wrap()。无论如何,如果您想使用字段值而不是 bean 序列化策略,则无论如何都需要这样做。

另外,您可能需要a RealmList parceler configuration

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-08-18
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
相关资源
最近更新 更多