英文:
java.lang.IllegalArgumentException: Ilegal value, when I try to pass object via bundle
问题
以下是翻译好的内容:
我正试图将一个来自Java类"Transfer"的对象传递到Kotlin类"Limit",如果我尝试使用data.name通过"Bundle"获取传递的值,编译器不会报错,但是当我进入这个问题所在的活动时,我该如何通过Bundle()在我的意图中获取一个对象?
public class Transfer {
private void sendBundle(){
Usermodel doTransfer = fillUserModel();
Limit.intentTransfer(doTransfer, "France");
}
private UserModel fillUserModel() {
Usermodel newUserModel = new Usermodel();
usermodel.setName("Jonas");
userModel.setAge("30");
usermodel.setIdNumber("123458");
userModel.setOccupation("dev");
userModel.setFormation("CC");
return newUserModel ;
}
}
class UserModel(
val name: String? = "",
val age: String? = "",
val idNumber: String? = "",
val occupation: String? = "",
val formation: String? = "",
)
class Limit {
private val data: Usermodel by bindBundle(DATA)
private val country: String by bindBundle(COUNTRY)
override fun onCreate(savedInstanceState: Bundle?) {
//here I can get the values using data.name or data.age
// and android studio does not point out error
}
companion object {
const val DATA = "data";
const val COUNTRY= "CountryUser";
}
fun intentTransfer (test : UserModel, CountryUser : String)
: Intent {
return Intent(context, Limit::class.java).apply {
putExtras(
BundleOf(
DATA to test,
COUNTRY to CountryUser
)
)
}
}
OUTPUT
WHEN I ENTER ACTIVITY:
java.lang.IllegalArgumentException: Ilegal value type android.model.UserModel for key "data"
英文:
I'm trying to pass an object from the class "Transfer" (java) to the class "Limit" (Kotlin), if I try to get the values that come through the "Bundle" using data.name, the compiler does not give an error, but when I enter in the activity of this problem, how can I get an object via Bundle () in my intent?
public class Transfer {
private void sendBundle(){
Usermodel doTransfer = fillUserModel();
Limit.intentTransfer(doTransfer, "France");
}
private UserModel fillUserModel() {
Usermodel newUserModel = new Usermodel();
usermodel.setName("Jonas");
userModel.setAge("30");
usermodel.setIdNumber("123458");
userModel.setOccupation("dev");
userModel.setFormation("CC");
return newUserModel ;
}
}
class UserModel(
val name: String? = "",
val age: String? ="",
val idNumber: String? ="",
val occupation: String? ="",
val formation: String? ="",
)
class Limit {
private val data: Usermodel by bindBundle(DATA)
private val country: String by bindBundle(COUNTRY)
override fun onCreate(savedInstanceState: Bundle?) {
//here I can get the values using data.name or data.age
// and android studio does not point out error
}
companion object {
const val DATA = "data"
const val COUNTRY= "CountryUser"
}
fun intentTransfer (test : UserModel, CountryUser : String)
: Intent {
return Intent(context, Limit::class.java).apply {
putExtras(
BundleOf(
DATA to test,
COUNTRY to CountryUser
)
)
}
}
OUTPUT
WHEN I ENTER ACTIVITY:
java.lang.IllegalArgumentException: Ilegal value type android.model.UserModel for key "data"
答案1
得分: 6
你需要创建一个实现了 Parcelable
接口的类,该类的对象将通过 Bundle
进行传递。
在你的 gradle 文件中:
apply plugin: 'org.jetbrains.kotlin.android.extensions'
你的数据类应该像这样:
@Parcelize
data class UserModel(
val name: String? = "",
val age: String? = "",
val idNumber: String? = "",
val occupation: String? = "",
val formation: String? = ""
) : Parcelable
英文:
You will need to make the class Parcelable
whose object you are passing through the Bundle
.
In your gradle:
apply plugin: 'org.jetbrains.kotlin.android.extensions'
Your data class should be like:
@Parcelize
data class UserModel(
val name: String? = "",
val age: String? ="",
val idNumber: String? ="",
val occupation: String? ="",
val formation: String? ="",
) : Parcelable
</details>
# 答案2
**得分**: 3
你的 `UserModel` 类无法被捆绑包识别为可序列化或可传递。尝试实现其中一个,错误将消失。
<details>
<summary>英文:</summary>
Your `UserModel` class cannot be recognized by bundle as a Serializable or Parcelable. Try to implement one of them and error will disappear.
</details>
# 答案3
**得分**: 2
我花了半天时间来解决这个问题。我的应用因为相同的错误而崩溃。
但是我使用了`kotlinx.serialization`的注解,甚至无法想象我还需要额外提及序列化的事情。
即使AS(Android Studio)也能将这个参数视为可序列化的!
只需在代码中添加`java.io.Serializable`,问题就解决了。
<details>
<summary>英文:</summary>
I spent half day with this issue. My app crashed with same error.
But I use kotlinx.serialization annotations and could not even imagine that I need additionally mention about Serialization
Even AS can see this parameter as Serializable!
Just added `java.io.Serializable` to the code and fixed[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/ae3pA.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论