IllegalStateException:parcel.readString()不能为null

时间:2017-08-15 07:27:15

标签: android android-intent kotlin parcelable

我将Parcelable对象放入Intent下一个Activity

val intent = Intent(mContext, ShowTestActivity::class.java)
intent.putExtra("test", test)
Log.d("myLog", "${intent.getParcelableExtra<Test>("test")}") // No problem
mContext.startActivity(intent)

在下一个Activity中获取此对象:

if (!intent.hasExtra("test")) throw Exception("Intent doesn't has 'test' extra")
val test: Test = intent.getParcelableExtra("test")

以下是抛出异常:

public open class QBase(): Parcelable {
  ...
  protected constructor(parcel: Parcel?) : this() {
      parcel?.let {
          question = parcel.readString() // parcel.readString() must not be null
          helpText = parcel.readString()
          qValue = parcel.readDouble()
          qType = QType.valueOf(parcel.readString())
      }
  }
  override fun writeToParcel(parcel: Parcel, flags: Int) {
      parcel.writeString(question)
      parcel.writeString(helpText)
      parcel.writeDouble(qValue)
      parcel.writeString(qType.name)
 }
  ...
}

在测试中:

public class Test(): Parcelable {
  ...
  public var questions: ArrayList<QBase> = ArrayList()

  constructor(parcel: Parcel?): this() {
      parcel?.let {
        ...
        parcel.readTypedList(questions, QBase.CREATOR)
      }
  }

  override fun writeToParcel(p: Parcel?, p1: Int) {
      p?.let {
          ...
          it.writeTypedList(questions)
      }
   }
   ...
}

这是一个例外:

  

引起:java.lang.IllegalStateException:parcel.readString()不能为null                                                                                      在com.vadim.hasdfa.justlearn.Model.Questions.utils.QBase。(QBase.kt:22)                                                                                      在com.vadim.hasdfa.justlearn.Model.Questions.utils.QBase $ CREATOR.createFromParcel(QBase.kt:70)                                                                                      在com.vadim.hasdfa.justlearn.Model.Questions.utils.QBase $ CREATOR.createFromParcel(QBase.kt:68)                                                                                      在android.os.Parcel.readTypedList(Parcel.java:2432)                                                                                      在com.vadim.hasdfa.justlearn.Model.Test。(Test.kt:28)                                                                                      在com.vadim.hasdfa.justlearn.Model.Test $ CREATOR.createFromParcel(Test.kt:47)                                                                                      在com.vadim.hasdfa.justlearn.Model.Test $ CREATOR.createFromParcel(Test.kt:45)                                                                                      在android.os.Parcel.readParcelable(Parcel.java:2787)                                                                                      在android.os.Parcel.readValue(Parcel.java:2681)                                                                                      在android.os.Parcel.readArrayMapInternal(Parcel.java:3048)                                                                                      在android.os.BaseBundle.unparcel(BaseBundle.java:257)                                                                                      在android.os.BaseBundle.containsKey(BaseBundle.java:435)                                                                                      在android.content.Intent.hasExtra(Intent.java:6859)                                                                                      在com.vadim.hasdfa.justlearn.Controller.Activity.BrowseTest.ShowTestActivity.onCreate(ShowTestActivity.kt:28)                                                                                      在android.app.Activity.performCreate(Activity.java:6980)                                                                                      在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)                                                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)                                                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                                      在android.app.ActivityThread.-wrap11(未知来源:0)                                                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                      在android.os.Looper.loop(Looper.java:164)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6540)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

3 个答案:

答案 0 :(得分:1)

question = parcel.readString() // parcel.readString() must not be null

也许您定义的字段question是一个非空字段,如果parcel.readString()的结果返回空值,则会抛出java.lang.IllegalStateException,只需更改val question: String插入val question: String?,然后重试。

答案 1 :(得分:0)

基于answer - 目前,唯一的解决方法是使实现parcelable的类的成员可以为空。

答案 2 :(得分:0)

因为您的字符串是空字符串,例如问题=“ null”