Skip to content
Advertisement

java.lang.IllegalArgumentException: Ilegal value, when I try to pass object via bundle

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"

Advertisement

Answer

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
Advertisement