gpt4 book ai didi

java - 将 Kotlin Parcelable Data Class 放入 java 包中不起作用

转载 作者:行者123 更新时间:2023-12-02 01:19:12 26 4
gpt4 key购买 nike

我正在处理一个同时使用 Java 和 Kotlin 的 Android 项目,我有一个实现了 Parcelable 的 Kotlin 数据类(此类中的一些属性是自定义 Java POJO 类,并且它也实现了 Parcelable)。

当我尝试在编译时将此类作为 Intent Parcelable Extra 传递时,没有错误,但是当我尝试在另一个 Activity 中检索此类时Intent.getExtras().getParcelable("Key") 它抛出异常

E/Parcel: Class not found when unmarshalling: 
java.lang.ClassNotFoundException

所以我尝试寻找异常的原因并尝试了 this question 中的所有答案但没有任何效果

1 - Koltin 数据类


data class OrderResponse (
@SerializedName("client")
var client: RideResponse? = null,

@SerializedName("driver")
var driver: BigRideResponse? = null,

@SerializedName("id")
var id: String? = null,
@SerializedName("rideInfo")
var rideInfo: RideDetails? = null
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readParcelable(RideResponse::class.java.classLoader),
parcel.readParcelable(BigRideResponse::class.java.classLoader),
parcel.readString(),
parcel.readParcelable(RideDetails::class.java.classLoader)) {
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(client, flags)
parcel.writeParcelable(driver, flags)
parcel.writeString(id)
parcel.writeParcelable(rideInfo, flags)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<OrderResponse> {
override fun createFromParcel(parcel: Parcel): OrderResponse {
return OrderResponse(parcel)
}

override fun newArray(size: Int): Array<OrderResponse?> {
return arrayOfNulls(size)
}
}
}

2-RideResponse


public class RideResponse implements Parcelable {

private String id;

private User user;

private OrderLocation location;

private RideInfo rideInfo;

private String orderType;

private String repeatedDays ;

private boolean repeated;

public boolean isRepeated() {
return repeated;
}

public void setRepeated(boolean repeated) {
this.repeated = repeated;
}

public String getRepeatedDays() {
return repeatedDays;
}

public void setRepeatedDays(String repeatedDays) {
this.repeatedDays = repeatedDays;
}




public RideResponse() {
}


protected RideResponse(Parcel in) {
id = in.readString();
user = in.readParcelable(User.class.getClassLoader());
location = in.readParcelable(OrderLocation.class.getClassLoader());
rideInfo = in.readParcelable(RideInfo.class.getClassLoader());
orderType = in.readString();
repeatedDays = in.readString();
repeated = in.readByte() != 0;

}

public static final Creator<RideResponse> CREATOR = new Creator<RideResponse>() {
@Override
public RideResponse createFromParcel(Parcel in) {
return new RideResponse(in);
}

@Override
public RideResponse[] newArray(int size) {
return new RideResponse[size];
}
};

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public OrderLocation getLocation() {
return location;
}

public void setLocation(OrderLocation location) {
this.location = location;
}

public RideInfo getRideInfo() {
return rideInfo;
}

public void setRideInfo(RideInfo rideInfo) {
this.rideInfo = rideInfo;
}

public String getOrderType() {
return orderType;
}

public void setOrderType(String orderType) {
this.orderType = orderType;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

dest.writeString(id);
dest.writeParcelable(user, flags);
dest.writeParcelable(location, flags);
dest.writeParcelable(rideInfo, flags);
dest.writeString(orderType);
dest.writeString(repeatedDays);
dest.writeByte((byte) (repeated ? 1 : 0));

}

}

3- BigRideResponseRideResponse 相同,但具有额外字段

4-传递 Parcelable 的代码

Intent intent = new Intent(getContext(), RatingActivity.class);
intent.putExtra(ApplicationConstants.ExtrasTag.ORDER_RESPONSE, mOrderResponse);
getViewCallback().openRatingScreen(intent, REQUEST_CODE_RATE);

5 - 检索 Parcelable 的代码

mOrderResponse = intent.getExtras().getParcelable(ApplicationConstants.ExtrasTag.ORDER_RESPONSE);

在浪费了一些时间之后,我尝试将 Kotlin 数据类(OrderResponse) 转换为常规 Java POJO 并实现了 Parcelable,并且效果很好!

所以我猜测将 Parcelable 数据类与 Java Bundle 一起使用似乎不起作用,所以我想知道到底为什么? kotlin 类不是映射到 Java 类,因此它们是相同的吗?

最佳答案

您需要将对象作为 Parcelable 发送,以通过 Intent 发送 Data 类对象,如下所示:

这里我使用 Kotlin 创建 Intent 对象

val intent = Intent(context, RatingActivity::class.java)
intent.putParcelableArrayListExtra(ApplicationConstants.ExtrasTag.ORDER_RESPONSE, mOrderResponse)

现在需要像这样将结果作为 Parcelable 获取。

确保像这样创建数据类的对象并获取对象中的 Intent 值

//object create of ArrayList
private var mOrderResponseViaIntent= ArrayList<OrderResponse>()


mOrderResponseViaIntent= intent.getParcelableArrayListExtra<OrderResponse>(ApplicationConstants.ExtrasTag.ORDER_RESPONSE)

关于java - 将 Kotlin Parcelable Data Class 放入 java 包中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com