当我在 Kotlin 中为我的项目使用 Parcelable 时,为什么会抛出 NullPointerException 错误?
why it is throw error as NullPointerException when I using Parcelable for my project in Kotlin?
我有一个小的聊天应用程序,我尝试在我的项目中使用 Parcelable,在调试项目之前没有任何错误。当我调试项目时,它抛出一个错误
setInfo(chat)
return participants.find { it.id != userId }!!.profilePicture
as NullPointerException,不知为何?有什么想法吗?
聊天活动:
val chat = intent.extras!!.getParcelable<Chat>("chat")!!
setInfo(chat)
configureRecycler(chat.messages)
private fun configureRecycler(messages: ArrayList<Message>) {
val user = userPresenter.getUser()
val messagesAdapter = MessagesAdapter(user.id, messages)
recycler_chat.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = messagesAdapter
}
}
用户:
import android.os.Parcelable;
import kotlinx.android.parcel.Parcelize
@Parcelize
class User(
val username: String?,
val profilePicture: String?
): BaseModel(), Parcelable {
constructor() : this("", "",)
}
聊天:
导入 android.os.Parcelable;
导入 kotlinx.android.parcel.Parcelize
@Parcelize
class Chat(
val participantsId: ArrayList<String>,
): BaseModel(), Parcelable {
var participants = ArrayList<User>()
val messages = ArrayList<Message>()
fun getProfilePicture(userId: String): String? {
return participants.find { it.id != userId }!!.profilePicture
}
fun getChatName(userId: String): String? {
return participants.find { it.id != userId }!!.username
}
}
留言:
导入 android.os.Parcelable;
导入 kotlinx.android.parcel.Parcelize
@Parcelize
class Message(
val ownerId: String,
val owner: User?
): BaseModel(), Parcelable {
val status: MessageStatus = MessageStatus.CREATED
}
问题不在 Parcelable 中。检查两次是否正确传递了 chat parcelable 对象。出于某种原因,聊天没有从之前的 activity 传递过来。您可以通过使用安全调用运算符来避免错误。
val chat: Chat? = intent.extras?.getParcelable<Chat>("chat")
chat?.let{ it->
setInfo(it)
configureRecycler(it.messages)
}
private fun configureRecycler(messages: ArrayList<Message>) {
val user = userPresenter.getUser()
val messagesAdapter = MessagesAdapter(user.id, messages)
recycler_chat.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = messagesAdapter
}
}
我有一个小的聊天应用程序,我尝试在我的项目中使用 Parcelable,在调试项目之前没有任何错误。当我调试项目时,它抛出一个错误
setInfo(chat)
return participants.find { it.id != userId }!!.profilePicture
as NullPointerException,不知为何?有什么想法吗?
聊天活动:
val chat = intent.extras!!.getParcelable<Chat>("chat")!!
setInfo(chat)
configureRecycler(chat.messages)
private fun configureRecycler(messages: ArrayList<Message>) {
val user = userPresenter.getUser()
val messagesAdapter = MessagesAdapter(user.id, messages)
recycler_chat.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = messagesAdapter
}
}
用户:
import android.os.Parcelable;
import kotlinx.android.parcel.Parcelize
@Parcelize
class User(
val username: String?,
val profilePicture: String?
): BaseModel(), Parcelable {
constructor() : this("", "",)
}
聊天: 导入 android.os.Parcelable; 导入 kotlinx.android.parcel.Parcelize
@Parcelize
class Chat(
val participantsId: ArrayList<String>,
): BaseModel(), Parcelable {
var participants = ArrayList<User>()
val messages = ArrayList<Message>()
fun getProfilePicture(userId: String): String? {
return participants.find { it.id != userId }!!.profilePicture
}
fun getChatName(userId: String): String? {
return participants.find { it.id != userId }!!.username
}
}
留言: 导入 android.os.Parcelable; 导入 kotlinx.android.parcel.Parcelize
@Parcelize
class Message(
val ownerId: String,
val owner: User?
): BaseModel(), Parcelable {
val status: MessageStatus = MessageStatus.CREATED
}
问题不在 Parcelable 中。检查两次是否正确传递了 chat parcelable 对象。出于某种原因,聊天没有从之前的 activity 传递过来。您可以通过使用安全调用运算符来避免错误。
val chat: Chat? = intent.extras?.getParcelable<Chat>("chat")
chat?.let{ it->
setInfo(it)
configureRecycler(it.messages)
}
private fun configureRecycler(messages: ArrayList<Message>) {
val user = userPresenter.getUser()
val messagesAdapter = MessagesAdapter(user.id, messages)
recycler_chat.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = messagesAdapter
}
}