从片段运行此代码片段时出现此错误HomeFragment
public void onTaskClick(View view, Need need, int position) {
Log.d(TAG, "onTaskClick() - " + need.toString());
Intent intent = new Intent(getActivity(), TaskDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable(TaskDetailsActivity.EXTRA_OBJ_NEED, Parcels.wrap(need));
startActivity(intent, bundle);
}
这是需要.java
@Parcel(implementations = {NeedRealmProxy.class}, value = Parcel.Serialization.FIELD, analyze = { Need.class })
public class Need extends RealmObject implements Serializable {
@PrimaryKey
@SerializedName("_id")
private String id;
@SerializedName("created")
private Date created;
@SerializedName("status")
private String status;
@SerializedName("fees")
float fees;
@SerializedName("user")
private User user;
@ParcelPropertyConverter(RealmListParcelConverter.class)
@SerializedName("places")
private RealmList<Place> places;
@SerializedName("user_location")
private Place userLocation;
@ParcelPropertyConverter(RealmListParcelConverter.class)
@SerializedName("items_ordered")
private RealmList<ItemOrdered> itemsOrdered;
@ParcelPropertyConverter(RealmListParcelConverter.class)
@SerializedName("movings")
private RealmList<Moving> movings;
@ParcelPropertyConverter(RealmListParcelConverter.class)
@SerializedName("messages")
private RealmList<Message> messages;
@SerializedName("run")
private Run run;
public Need() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Place> getPlaces() {
return places;
}
public List<ItemOrdered> getItemsOrdered() {
return itemsOrdered;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Double getAmount() {
double p = 0;
for (ItemOrdered e: itemsOrdered) {
if (e.getItem().getPrice() != null) {
p += e.getItem().getPrice() * e.getQuantity();
}
}
return p;
}
public Place getUserLocation() {
return userLocation;
}
public void setUserLocation(Place userLocation) {
this.userLocation = userLocation;
}
public Date getCreated() {
return created;
}
public String getCreatedFormatted() {
SimpleDateFormat format = new SimpleDateFormat("MMM dd, HH:mm");
return format.format(created);
}
public void setCreated(Date created) {
this.created = created;
}
public String getStatus() {
return status;
}
public List<Message> getMessages() {
return messages;
}
public Run getRun() {
return this.run;
}
public List<Moving> getMovings() {
return movings;
}
public void setMovings(RealmList<Moving> movings) {
this.movings = movings;
}
public float getFees() {
return fees;
}
public void setFees(float fees) {
this.fees = fees;
}
public void setPlaces(RealmList<Place> places) {
this.places = places;
}
public void setItemsOrdered(RealmList<ItemOrdered> itemsOrdered) {
this.itemsOrdered = itemsOrdered;
}
public void setMessages(RealmList<Message> messages) {
this.messages = messages;
}
@Override
public String toString() {
return "Need{" +
"id= '" + id + ''' +
", movings='" + movings.size() + ''' +
", places=" + places.size() +
", itemsOrdered=" + itemsOrdered.size() +
", userLocation=" + userLocation.getLng() + "," + userLocation.getLat() +
", user=" + user +
'}';
}
}
这是RealmListParcelConverter.java
public class RealmListParcelConverter implements
TypeRangeParcelConverter<RealmList<? extends RealmObject>, RealmList<? extends RealmObject>> {
private static final int NULL = -1;
@Override
public void toParcel(RealmList<? extends RealmObject> input, android.os.Parcel parcel) {
parcel.writeInt(input == null ? NULL : input.size());
if (input != null) {
for (RealmObject item : input) {
parcel.writeParcelable(Parcels.wrap(item), 0);
}
}
}
@Override
public RealmList fromParcel(android.os.Parcel parcel) {
int size = parcel.readInt();
RealmList list = new RealmList();
for (int i=0; i<size; i++) {
Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader());
list.add((RealmObject) Parcels.unwrap(parcelable));
}
return list;
}
}
错误
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: io.realm.NeedRealmProxy$$Parcelable at android.os.Parcel.readException(Parcel.java:1427) at android.os.Parcel.readException(Parcel.java:1379) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1761) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1411) at android.app.Activity.startActivityForResult(Activity.java:3351) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 在 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 在 android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30) 在 android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146) 在 android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932) 在 android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047) at android.support.v4.app.Fragment.startActivity(Fragment.java:940) 在 com.nkapsi.ui.fragment.HomeFragment.onTaskClick(HomeFragment.java:201)
您应该传递 id 并将对象从另一侧的 id 拉出。
像这样:
public void onTaskClick(View view, Need need, int position) {
Log.d(TAG, "onTaskClick() - " + need.toString());
Intent intent = new Intent(getActivity(), TaskDetailsActivity.class);
intent.putExtra("need_id", need.getId());
startActivity(intent);
}
然后在接收 id 的活动中,将对象取回。
// in onCreate(), onHandleIntent(), etc.
// get the needId back out.
String needId = intent.getStringExtra("need_id");
// get the object out by id.
realm.where(Need.class).equalTo("id", needId).findFirst();
// Lookup by PK is super fast, but if you want you can always use
realm.where(Need.class).equalTo("id", needId).findFirstAsync();
// This will make this microsecond lookup happen on a background thread
// and return the object back to you on your main thread, but is
// probably overkill for a single object.
更多关于这个 (https://realm.io/docs/java/latest/#intents)
do
@Parcel(value = Parcel.Serialization.FIELD, analyze = { Need.class })
和
bundle.putParcelable(TaskDetailsActivity.EXTRA_OBJ_NEED, Parcels.wrap(realm.copyFromRealm(need)));
P.S. 通常你只需要发送对象的主键,而不是整个对象。
但是基于领域Java问题,显然您需要遵循proguard配置
-keepnames public class * extends io.realm.RealmObject
RealmObject 不能序列化。因此,您将无法使用 put extra 传递任何 RealmObject。在这种情况下,你应该传递 RealmObject 的 id。在目标活动中,您应该使用此 id 查询 RealmObject。正如@Eric麦克斯韦回答的那样。