从片段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);
}
这是 Need.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时 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) 在android.app.Activity.startActivityForResult(Activity.java:3351)处 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) 在android.support.v4.app.Fragment.startActivity(Fragment.java:940) 在 com.nkapsi.ui.fragment.HomeFragment.onTaskClick(HomeFragment.java:201)
答案 0 :(得分:3)
你应该传递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的Activity中,将对象取回。
// 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)
答案 1 :(得分:0)
待办事项
@Parcel(value = Parcel.Serialization.FIELD, analyze = { Need.class })
和
bundle.putParcelable(TaskDetailsActivity.EXTRA_OBJ_NEED, Parcels.wrap(realm.copyFromRealm(need)));
P.S。通常你只需要发送对象的主键,而不是整个对象。
但基于realm-java问题,显然你需要遵循proguard配置
-keepnames public class * extends io.realm.RealmObject
答案 2 :(得分:0)
RealmObject不能是Serializable。所以你将无法使用put extra传递任何RealmObject。在这种情况下,您应该传递RealmObject的id。在目标Activity中,您应该使用此id查询RealmObject。正如@Eric Maxwell回答的那样。