将带有包裹的领域对象传递给 activity 有 return 空

Passing realmobject with parcel to activity has return null

任何人都可以帮助我解决我将带有包裹的领域对象传递给另一个 activity 并且在第二个 activity 我的对象为空的问题吗?

在 Activity 中,我从标签中获取 class 类别的 instatne。

        Intent intent = new Intent(context, CategoryListActivity.class);
        Category category = (Category)v.getTag();
        Log.e("Id", " "+category.getId()); //this is ok it prints 1
        Parcelable parcelable = Parcels.wrap(category);

        intent.putExtra("category", parcelable);
        startActivity(intent); 

并且在 class CategoryListActivity v 方法 onCreate 是代码

Intent intent = getIntent();
Category category = Parcels.unwrap(intent.getParcelableExtra("category"));
Category category1= Parcels.unwrap(getIntent().getExtras().getParcelable("category"));
Log.e("Id 1", " "+category.getId()); //retun 0
Log.e("Id 1", " "+category1.getId()); //return 0

打印出来的是 0,我不知道为什么是 0。谁能提出解决这个问题的建议? thx,或者如果需要插入我的扩展 RealmObject 的实体,我可以粘贴

您不应在包裹中发送 RealmObject,除非它们是非托管对象或独立对象。

只发送 ID 值并在另一端查询对象会更安全:https://realm.io/docs/java/latest/#intents

如果你想使用Parceler。您是否按照此处所述配置了对象:https://realm.io/docs/java/latest/#parceler