Parcelable 重载解析歧义
Parcelable overload resolution ambiguity
我正在尝试在 Kotlin 中创建 JSON 响应的 POJO(在 Kotlin 中也称为数据 classes)结构。我已经为结构中的每个数据 class 实现了 Parcelable 接口。在所有数据 classes 中,我已经自动生成了 Parcelable 实现。问题是生成的第二个构造函数,其中 IDE 抱怨:
Overload resolution ambiguity
它指出这两个构造函数之间存在混淆:
public constructor GeocodeRes(parcel: Parcel)
public constructor GeocodeRes(responset: ResponseRes)
我认为这是有道理的,因为 ResponseRes 也是 Parcelable 类型(ResponseRes 实现了 Parcelable)。因此调用 GeocodeRes(parcel) 方法(在 createFromParcel 配套方法中)时,它变得很混乱。
直到我从实现 Parcelable class 中删除了 ResponseRes,它仍然显示相同的错误。
这有什么原因吗?我设置正确吗?在所有子数据 class 中,它们都实现了 Parcelable 接口(相互依赖),但没有 运行 出现任何问题。
这是我的 GeocodeRes class:
import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
data class GeocodeRes(
@SerializedName("Response") @Expose val responset: ResponseRes
) : Parcelable {
// this is the problem. the IDE is complaining that the usage is too ambiguous (). however, the only usage of this constructor is within this class - just doesn't tell me where exactly.
constructor(parcel: Parcel) : this(parcel.readParcelable(ResponseRes::class.java.classLoader)) {
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(responset, flags)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<GeocodeRes> {
override fun createFromParcel(parcel: Parcel): GeocodeRes {
return GeocodeRes(parcel)
}
override fun newArray(size: Int): Array<GeocodeRes?> {
return arrayOfNulls(size)
}
}
}
这是我的 ResponseRes class:
data class ResponseRes(
@SerializedName("MetaInfo") @Expose val metaInfo: MetaInfo,
@SerializedName("View") @Expose val views: List<View>
): Parcelable
{
[...]//parcel methods
}
however, the only usage of this constructor is within this class - just doesn't tell me where exactly
问题出在定义本身,而不是任何用法。永远无法使用,错误依然存在
您应该可以通过指定您想要阅读的Parcelable
来解决此问题:
this(parcel.readParcelable<ResponseRes>(ResponseRes::class.java.classLoader))
编译器无法判断你的意思是那个还是
this(parcel.readParcelable<Parcel>(ResponseRes::class.java.classLoader))
即使第二个不合法,因为 Parcel
没有实现 Parcelable
,如果您查看签名
<T extends Parcelable> T readParcelable(ClassLoader loader)
你可以看到只有return类型可以用来推断T
,而不是参数。因此,编译器需要在 尝试推断 T
.
之前选择构造函数重载
我正在尝试在 Kotlin 中创建 JSON 响应的 POJO(在 Kotlin 中也称为数据 classes)结构。我已经为结构中的每个数据 class 实现了 Parcelable 接口。在所有数据 classes 中,我已经自动生成了 Parcelable 实现。问题是生成的第二个构造函数,其中 IDE 抱怨:
Overload resolution ambiguity
它指出这两个构造函数之间存在混淆:
public constructor GeocodeRes(parcel: Parcel)
public constructor GeocodeRes(responset: ResponseRes)
我认为这是有道理的,因为 ResponseRes 也是 Parcelable 类型(ResponseRes 实现了 Parcelable)。因此调用 GeocodeRes(parcel) 方法(在 createFromParcel 配套方法中)时,它变得很混乱。
直到我从实现 Parcelable class 中删除了 ResponseRes,它仍然显示相同的错误。
这有什么原因吗?我设置正确吗?在所有子数据 class 中,它们都实现了 Parcelable 接口(相互依赖),但没有 运行 出现任何问题。
这是我的 GeocodeRes class:
import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
data class GeocodeRes(
@SerializedName("Response") @Expose val responset: ResponseRes
) : Parcelable {
// this is the problem. the IDE is complaining that the usage is too ambiguous (). however, the only usage of this constructor is within this class - just doesn't tell me where exactly.
constructor(parcel: Parcel) : this(parcel.readParcelable(ResponseRes::class.java.classLoader)) {
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(responset, flags)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<GeocodeRes> {
override fun createFromParcel(parcel: Parcel): GeocodeRes {
return GeocodeRes(parcel)
}
override fun newArray(size: Int): Array<GeocodeRes?> {
return arrayOfNulls(size)
}
}
}
这是我的 ResponseRes class:
data class ResponseRes(
@SerializedName("MetaInfo") @Expose val metaInfo: MetaInfo,
@SerializedName("View") @Expose val views: List<View>
): Parcelable
{
[...]//parcel methods
}
however, the only usage of this constructor is within this class - just doesn't tell me where exactly
问题出在定义本身,而不是任何用法。永远无法使用,错误依然存在
您应该可以通过指定您想要阅读的Parcelable
来解决此问题:
this(parcel.readParcelable<ResponseRes>(ResponseRes::class.java.classLoader))
编译器无法判断你的意思是那个还是
this(parcel.readParcelable<Parcel>(ResponseRes::class.java.classLoader))
即使第二个不合法,因为 Parcel
没有实现 Parcelable
,如果您查看签名
<T extends Parcelable> T readParcelable(ClassLoader loader)
你可以看到只有return类型可以用来推断T
,而不是参数。因此,编译器需要在 尝试推断 T
.