Kotlin parcelable类抛出ClassNotFoundException
我有一个类作为我的RecyclerView
数据模型,以便将这个类的对象从一个活动传递给另一个活动,通过Intent
我必须使它可以Parcelable
现在的问题是,我能够将对象从一个活动发送到另一个活动并检索它, 以便应用程序不会崩溃 ,但会一直抛出ClassNotFoundException
在logcat屏幕中。
我究竟做错了什么?
—-> Person.kt
@Parcelize class Person(var name: String, var username: String, var address: String, val avatar: Int) : Parcelable
—->在MainActivity.kt
val intent = Intent(this, ProfilePage::class.java) intent.putExtra("clicked_person",person) startActivity(intent)
—->。 onCreate()
在ProfilePAge.kt中
var person = intent.getParcelableExtra<Person>("clicked_person") as Person
和Exception
E/Parcel: Class not found when unmarshalling: com.example.testkot.kotlinapp.Person java.lang.ClassNotFoundException: com.example.testkot.kotlinapp.Person
请记住,该应用程序不会崩溃,它继续工作,但在logcat中显示异常
在评论中测试解决方案之后,以下工作不会引发任何异常
通过Bundle
发送Parcelable
val intent = Intent(this, ProfilePage::class.java) var bundle = Bundle() bundle.putParcelable("selected_person",person) intent.putExtra("myBundle",bundle) startActivity(intent)
恢复可Parcelable
val bundle = intent.getBundleExtra("myBundle") var person = bundle.getParcelable<Person>("selected_person") as Person
但是,我不知道在旧的代码在这个问题上的差异,为什么旧的代码抛出异常,为什么这个新的不抛出