2

从片段运行这段代码时出现此错误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:io.realm.NeedRealmProxy$$Parcelable 在 android.os.Parcel.readException(Parcel.java:1427) 在 android.os.Parcel.readException(Parcel.java:1379) 在 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$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047) 在 android.support.v4.app 在 android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932) .Fragment.startActivity(Fragment.java:940) 在 com.nkapsi.ui.fragment.HomeFragment.onTaskClick(HomeFragment.java:201)

4

3 回答 3

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

于 2017-04-13T20:21:20.540 回答
0

RealmObject 不能是可序列化的。因此,您将无法使用 put extra 传递任何 RealmObject。在这种情况下,您应该传递 RealmObject 的 id。在目标 Activity 中,您应该使用此 ID 查询 RealmObject。正如@Eric Maxwell 回答的那样。

于 2017-04-15T07:55:05.930 回答
0

@Parcel(value = Parcel.Serialization.FIELD, analyze = { Need.class })

bundle.putParcelable(TaskDetailsActivity.EXTRA_OBJ_NEED, Parcels.wrap(realm.copyFromRealm(need)));

PS 通常你只需要发送对象的主键,而不是整个对象。



但是基于 realm-java 问题,显然你需要遵循 proguard 配置

-keepnames public class * extends io.realm.RealmObject

于 2017-04-10T12:42:11.503 回答