将 Realm 对象转换为 Parcelable 时出错

Error Casting Realm Objects as Parcelable

我正在尝试在活动之间发送 Realm 对象。我读过使用 parcelable 是最好的方法。但是,我在尝试通过意图传递它时遇到了一个转换错误。

添加@Primary Key 产生了 "Primary key not found" 错误,所以我忽略了它。任何帮助将不胜感激!

我的领域对象class:

@org.parceler.Parcel( implementations = { PersonRealmProxy.class },
value = Parcel.Serialization.BEAN,
analyze = { Person.class })
public class Person extends RealmObject {

private String name;
private int ID;
private String last_name;
private String lots_to_write;
...//getters and setters start here

从 Main 传递 Realm 对象Activity:

Intent new_ticket = new Intent (MainActivity.this, AllAddedPeople.class );
            new_ticket.putExtra("copyRealm", (Parcelable) myRealm);
            startActivity(new_ticket);

...并在第 2 个收到它 Activity:

public class AllAddedPeople extends AppCompatActivity {

private Realm myRealm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_all_people);

    myRealm= (Realm) getIntent().getParcelableExtra("MyClass");

你的人 class 应该实现 Parcelable 接口然后你应该没问题。关于创建parcelable请参考官方文档class https://developer.android.com/reference/android/os/Parcelable.html

Intent new_ticket = new Intent (MainActivity.this,AllAddedPeople.class);
            new_ticket.putExtra("copyRealm", (Parcelable) myRealm);
            startActivity(new_ticket);

您不需要将 myRealm 引用转换为 Parcelable

应该是:

 Intent new_ticket = new Intent (MainActivity.this,AllAddedPeople.class);
                new_ticket.putExtra("copyRealm", myRealm);
                startActivity(new_ticket);

和其他 activity

private Realm myRealm;

应该是

private MyClass myRealm;

也改

 myRealm= (Realm) getIntent().getParcelableExtra("MyClass");

 myRealm= (MyClass) getIntent().getParcelableExtra("MyClass"); 

好的,所以 XY problem 的原始 Y 部分是,即使您谈到通过意图发送 Person 对象,您实际上是在尝试按原样发​​送 Realm 而不是一个 parcelable,这意味着当你说 (Parcelable)realm 时得到 ClassCastException 是完全有效的,因为它不可 parcelable。


您的 Y 问题的 解决方案 是您应该将 Parceler 与 RealmObject 的非托管版本一起使用。

@org.parceler.Parcel( /* removing implementations=... */
value = Parcel.Serialization.BEAN,
analyze = { Person.class })
public class Person extends RealmObject {

    // @PrimaryKey // <-- this is obviously missing
    private int ID;

    private String name;
    private String last_name;
    private String lots_to_write;
    ...//getters and setters start here

那你可以做

Intent new_ticket = new Intent (MainActivity.this, AllAddedPeople.class );
        new_ticket.putExtra("person", Parcels.wrap(person.copyFromRealm()));
        startActivity(new_ticket);

你可以这样收到

private Realm myRealm;
private Person person;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_all_people);

    myRealm = Realm.getDefaultInstance();
    // THIS IS WRONG: myRealm = getIntent().getParcelableExtra("MyClass");

    person = Parcels.unwrap(getIntent().getParcelableExtra("person")); // <-- unmanaged person obj.

但这实际上是错误的解决方案,X问题的解决方案是您不应该从领域,你不应该创建非托管的 RealmObject 实例;他们应该通过主键重新查询。

Intent new_ticket = new Intent (MainActivity.this, AllAddedPeople.class );
        new_ticket.putExtra("personId", person.getID());
        startActivity(new_ticket);

public class AllAddedPeople extends AppCompatActivity {

    private Realm myRealm;
    private Person person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_all_people);

        myRealm = Realm.getDefaultInstance();
        person = myRealm.where(Person.class)
                    .equalTo("ID", getIntent().getIntExtra("personId"))
                    .findFirst(); // <-- managed RealmObject
        person.addChangeListener(...