3
votes

I put Parcelable object in Intent to next Activity:

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

And get this object in next Activity:

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

And here are throwing Exception:

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)
 }
  ...
}

And in Test:

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)
      }
   }
   ...
}

It is an exception:

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

3
You could add Intent intent = getIntent(); in ShowTestActivity before you get object.KeLiuyue
In kotlin intent means getIntent() in JavaHasdfa
oh,sorry for my poor in Kotlin.KeLiuyue
Ok, I do it... And then all vars is nullHasdfa

3 Answers

2
votes

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

maybe the field question you defined is a not-null field, if the result of parcel.readString() returns a null value, there will be a java.lang.IllegalStateException throwed, just change the definition of val question: String into val question: String? and try again.

0
votes

Based on the answer - For the time being, the only workaround is to make the member of the class that implements parcelable to be nullable.

0
votes

Because your string is null string, like question="null"