gpt4 book ai didi

android-intent - Kotlin 可打包类抛出 ClassNotFoundException

转载 作者:IT老高 更新时间:2023-10-28 13:46:26 26 4
gpt4 key购买 nike

我有一个类用作 RecyclerView 的数据模型,以便通过 Intent 将此类的对象从一个事件传递到另一个事件它可打包

现在的问题是我能够将对象从一个事件发送到另一个事件并检索它,这样应用就不会崩溃,但会不断抛出 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)

---->。 ProfilePAge.kt 中的 onCreate()

var person  = intent.getParcelableExtra<Person>("clicked_person") as Person

异常

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

但是,我不知道我的旧代码在问题中的区别以及为什么旧代码会抛出异常以及为什么这个新代码不会抛出

关于android-intent - Kotlin 可打包类抛出 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46728857/

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