com.app.android.models.Item 不可序列化或可打包
尝试学习 kotlin,所以任何帮助将非常感激。我试图在片段之间传递这个对象并获取它不可序列化或不可分割的消息。关于为什么会发生这种情况有什么建议吗?
@Serializable
data class Item(
@SerialName("_version")
val version: String,
@SerialName("language")
val language: String,
@SerialName("region")
val region: String? = null,
@SerialName("variant")
val variant: String? = null
)
nav_graph.xml
<fragment
android:id="@+id/fragment1"
android:name="com.app.android.ui.fragment1">
<action
android:id="@+id/toFragment2"
app:destination="@id/Fragment2"
/>
<argument
android:name="item1"
android:defaultValue="@null"
app:argType="com.app.android.models.Item"
app:nullable="true" />
</fragment>
它允许我传递存储在数据部分而不是模型部分中的其他对象,因此不确定为什么这是一个问题。例如,这个对象可以作为参数:
@Parcelize
data class Item2 internal constructor(
val code: String,
val Id: String,
val url: String,
) : Parcelable {
companion object {
//more code
}
}
Trying learn kotlin so any help would really appreciate. I am trying to pass this object between fragments and getting the message it's not serializable or parcelable. Any suggestions on why this would happen?
@Serializable
data class Item(
@SerialName("_version")
val version: String,
@SerialName("language")
val language: String,
@SerialName("region")
val region: String? = null,
@SerialName("variant")
val variant: String? = null
)
nav_graph.xml
<fragment
android:id="@+id/fragment1"
android:name="com.app.android.ui.fragment1">
<action
android:id="@+id/toFragment2"
app:destination="@id/Fragment2"
/>
<argument
android:name="item1"
android:defaultValue="@null"
app:argType="com.app.android.models.Item"
app:nullable="true" />
</fragment>
It lets me pass other objects stored in the data section instead of the model section so not sure why this one is an issue. For example this object is fine as an argument:
@Parcelize
data class Item2 internal constructor(
val code: String,
val Id: String,
val url: String,
) : Parcelable {
companion object {
//more code
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该注释有助于编译器,但您仍然需要实现
Serialized
接口。Item2
起作用的原因是它实现了Parcelable
接口。The annotation helps the compiler, but you still need to implement the
Serializable
interface.The reason that
Item2
works is because it implements theParcelable
interface.