当前位置: 首页 > 知识库问答 >
问题:

解组时ClassNotFoundException:io.realm.NeedRealmProxy$$Parcelable

曾明诚
2023-03-14

当我从片段< code>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;
    }
}

错误

解组时出现异常:io . realm . needrealmproxy $ $ Parcelable at Android . OS . parcel . read exception(parcel . Java:1427)at Android . OS . parcel . parcel . read exception(parcel . Java:1379)at Android . app . activitymanagerproxy . start activity(activitymanagernative . Java:1761)at Android . app . instrumentation . execstart activity(instrumentation . Java:1411)

共有3个答案

闾丘正志
2023-03-14

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

巫马嘉祯
2023-03-14

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

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

P. S.通常您只需要发送对象的主键,而不是整个对象。

但基于领域java问题,显然您需要以下proguard配置

-keepnames公共类*扩展io.realm.RealmObject

艾凯捷
2023-03-14

您应该传递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)

 类似资料:
  • 我使用以下代码通过 JAXB 取消绑定 XML。responseXML 包含从 Web 服务调用返回的 XML 字符串。 以下是取消编组时发生的异常。 有人帮忙解决这个问题。 下面是从XSD自动生成的TestACK类 下面是示例XML

  • 当我在活动中获得意向性额外时,我有一个错误:

  • 早上好,我试图在我们的代码中找到一个错误,关于当xml没有被格式化时使用JAXB解组XML。我已经做了很多调试和测试,但仍然找不到错误。 xml的解释部分如下所示: 列表最多可包含50.000<代码> 为此,我们有以下代码。解组器的创建被移动到一个接口,以区别我们希望通过jaxb解组的元素,包括子元素(如元素)和那些(如

  • 我有一个XML字符串,如下所示: 现在,我用名称数据将其解组到类中。java,它将变量作为CustomerInfo的另一个类。JAVA 但在解组时,我得到了一个错误作为意外元素(uri:)https://www.google/com,本地:“数据”)。预期的元素为

  • 我已经生成java类使用从一个xsd,其中根元素是类型的。 jaxb生成的根元素是 当我尝试解组与该xsd对应的xml并强制转换JaxbElement时,它会引发一个强制转换异常: 片段:

  • 我的班级照片列表: 我的班级照片: