gpt4 book ai didi

当我尝试通过包传递对象时出现 java.lang.IllegalArgumentException : Ilegal value,

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

我正在尝试将一个对象从类“Transfer”(java) 传递到类“Limit”(Kotlin),如果我尝试使用 data.name 获取通过“Bundle”获得的值,编译器并没有报错,但是当我进入这个问题的activity时,我的intent中如何通过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
)
)
}
}

输出当我进入 Activity 时:

 java.lang.IllegalArgumentException: Ilegal value type android.model.UserModel for key "data"

最佳答案

您需要创建类 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

关于当我尝试通过包传递对象时出现 java.lang.IllegalArgumentException : Ilegal value,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63797893/

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