gpt4 book ai didi

android - 从SavedInstanceState获取自定义类型的列表时获取IllegalStateException

转载 作者:行者123 更新时间:2023-12-02 13:29:24 25 4
gpt4 key购买 nike

我有一个实现Parcelable的Question类,它充当myquiz.db和dbHelper类之间的桥梁。我要做的是在旋转设备后恢复状态。这是onSaveInstanceState()中的代码:

override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
super.onSaveInstanceState(outState, outPersistentState)
outState?.putInt(KEY_SCORE, score)
outState?.putInt(KEY_QUES_COUNT, questionCounter)
outState?.putBoolean(KEY_ANSWERED, answered)
outState?.putLong(KEY_MILLIS_LEFT, timeLeftInMillis)
outState?.putParcelableArrayList(KEY_QUES_LIST, list)
}

这是我从saveInstanceState还原列表的代码:
if (savedInstanceState == null) {
val dbHelper = QuizDbHelper(this)
list = dbHelper.getAllQuestions()
totalQuestions = list.size
list.shuffle()
textDefaultColor = rb1.textColors
textDefaultColorcd = timer.textColors
nextQuestion()
} else {
**list = savedInstanceState.getParcelableArrayList(KEY_QUES_LIST)**
totalQuestions = list.size
score = savedInstanceState.getInt(KEY_SCORE)
answered = savedInstanceState.getBoolean(KEY_ANSWERED)
questionCounter = savedInstanceState.getInt(KEY_QUES_COUNT)
currQuestion = list[questionCounter - 1]
timeLeftInMillis = savedInstanceState.getLong(KEY_MILLIS_LEFT)

if (!answered) {
startTimer()
} else {
updateCoundownText()
correctAnswer()
}
}

当我运行应用程序时,出现以下错误:elset块类型不匹配的 java.lang.IllegalStateException: savedInstanceState.getPa…eArrayList(KEY_QUES_LIST) must not be null第一行。另外,当我将鼠标指针悬停在此行时,它显示:
Required:
ArrayList<Question>
Found:
ArrayList<Question!>?

列表声明: private lateinit var list: ArrayList<Question>
问题类
class Question() : Parcelable {
private lateinit var question: String
private lateinit var option1: String
private lateinit var option2: String
private lateinit var option3: String
private var answerNr = 0

constructor(question: String, option1: String, option2: String, option3: String, answerNr: Int) : this() {
this.question = question
this.option1 = option1
this.option2 = option2
this.option3 = option3
this.answerNr = answerNr
}

constructor(parcel: Parcel) : this() {
parcel.readString()
parcel.readString()
parcel.readString()
parcel.readString()
parcel.readInt()
}


fun getQuestion(): String? {
return question
}

fun setQuestion(question: String) {
this.question = question
}

fun getOpt1(): String? {
return option1
}

fun setOpt1(option1: String) {
this.option1 = option1
}

fun getOpt2(): String? {
return option2
}

fun setOpt2(option2: String) {
this.option2 = option2
}

fun getOpt3(): String? {
return option3
}

fun setOpt3(option3: String) {
this.option3 = option3
}

fun getAnswerNo(): Int {
return answerNr
}

fun setAnswerNo(answerNr: Int) {
this.answerNr = answerNr
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(question)
parcel.writeString(option1)
parcel.writeString(option2)
parcel.writeString(option3)
parcel.writeInt(answerNr)
}

override fun describeContents(): Int {
return 0
}

companion object CREATOR : Parcelable.Creator<Question> {
override fun createFromParcel(parcel: Parcel): Question {
return Question(parcel)
}

override fun newArray(size: Int): Array<Question> {
return newArray(size)
}
}

}

最佳答案

在onSaveInstanceState方法中,您首先要调用super,然后再存储状态。因此list始终为null,并且您已将列表声明为不可为null,因此将引发异常。

更新代码:

 override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
outState?.putInt(KEY_SCORE, score)
outState?.putInt(KEY_QUES_COUNT, questionCounter)
outState?.putBoolean(KEY_ANSWERED, answered)
outState?.putLong(KEY_MILLIS_LEFT, timeLeftInMillis)
outState?.putParcelableArrayList(KEY_QUES_LIST, list)
super.onSaveInstanceState(outState, outPersistentState)
}

关于android - 从SavedInstanceState获取自定义类型的列表时获取IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62369005/

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