将Realm对象转换为Parcelable时出错

时间:2017-06-13 12:16:42

标签: android realm parcelable

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

添加@Primary Key创建了“未找到主键”错误,因此我省略了它。任何帮助将不胜感激!

My Realm Object类:

@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

从MainActivity传递Realm对象:

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

...并在第二个活动中接收它:

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");

3 个答案:

答案 0 :(得分:2)

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

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 问题的解决方案是你不应该从Realm复制RealmObjects,你不应该做非托管的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(...

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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);

和其他活动

private Realm myRealm;

应该是

private MyClass myRealm;

也改变了

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

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