android activity 简历上的 BadParcelableException

BadParcelableException on android activity resume

我有一个 activity 在它的 onResume 方法

中加载片段
override fun onResume() {
    super.onResume()

    loadDietFragment()
}

fun loadDietFragment() {
  val uri = Uri.parse("${DatabaseContract.CONTENT_URI}/current")
  val currentDietCursor = contentResolver.query(uri , null, null, null, null )
  if(currentDietCursor.count > 0) {
      currentDietCursor.moveToFirst()
      currentDiet = DietEntity(currentDietCursor)
      currentDietCursor.close()
      loadHomeFragment()
      return
  }
}

private fun loadHomeFragment() {
        val transaction = supportFragmentManager.beginTransaction()
        val fragment = HomeFragment()
        val arguments = Bundle()
        arguments.putParcelable("diet", currentDiet)
        fragment.arguments = arguments

        transaction.replace(R.id.HomeFrameLayout, fragment)

        transaction.addToBackStack(null)
        transaction.commit()
    }

然后在片段中我有:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        currentDiet = arguments.getParcelable("diet")
        preferences = PreferenceManager.getDefaultSharedPreferences(activity)
    }

它看起来很好并且正在工作,但在一些罕见的情况下,当我收到带有此堆栈跟踪的 BadParcelableException 时:

   java.lang.RuntimeException: 
     at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2560)
     at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2630)
     at android.app.ActivityThread.-wrap11 (ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1482)
     at android.os.Handler.dispatchMessage (Handler.java:111)
     at android.os.Looper.loop (Looper.java:207)
     at android.app.ActivityThread.main (ActivityThread.java:5736)
     at java.lang.reflect.Method.invoke (Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:907)
     at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:768)
   Caused by: android.os.BadParcelableException: 
     at android.os.Parcel.readParcelableCreator (Parcel.java:2415)
     at android.os.Parcel.readParcelable (Parcel.java:2337)
     at android.os.Parcel.readValue (Parcel.java:2243)
     at android.os.Parcel.readArrayMapInternal (Parcel.java:2592)
     at android.os.BaseBundle.unparcel (BaseBundle.java:221)
     at android.os.Bundle.getParcelable (Bundle.java:786)
     at com.healthier_life.a90_days_diet.fragments.HomeFragment.onCreate    (HomeFragment.kt:59)
     at android.support.v4.app.Fragment.performCreate (Fragment.java:2339)
     at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1377)
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState    (FragmentManager.java:1740)
     at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1809)
     at android.support.v4.app.FragmentManagerImpl.dispatchStateChange    (FragmentManager.java:3217)
     at android.support.v4.app.FragmentManagerImpl.dispatchCreate (FragmentManager.java:3166)
     at android.support.v4.app.FragmentController.dispatchCreate (FragmentController.java:181)
     at android.support.v4.app.FragmentActivity.onCreate (FragmentActivity.java:319)
     at android.support.v7.app.AppCompatActivity.onCreate (AppCompatActivity.java:84)
     at com.healthier_life.a90_days_diet.activities.BaseActivity.onCreate    (BaseActivity.kt:18)
     at com.healthier_life.a90_days_diet.MainActivity.onCreate (MainActivity.kt:46)
     at android.app.Activity.performCreate (Activity.java:6278)
     at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1136)
     at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2513)

我无法始终如一地重现它。它一天发生几次(Google 播放统计数据),我只能在我的 phone 上看到一次。我已经有几个小时没用过这个应用程序了。我点击了应用程序图标,它抛出了异常。

当前的例子有什么问题吗?我愿意听取有关如何修复错误或如何一致地重现错误的建议。 谢谢!

编辑: 我忘记分享 DietEntity class:https://pastebin.com/EkT79ubT 它在 pastebin 中,因为它不允许我将那么多代码和那么多文本放在 post 中。

Android Studio 实际上有一个用于 Parcelable 实现的代码生成选项。对我来说最突出的是你的 CREATOR 可能没有正确实现。尝试将其作为伴随对象来做。也不需要像 in 这样的保留字命名变量,并且必须一直使用反引号。

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeInt(id)
    parcel.writeLong(startDate.millis)
    parcel.writeLong(endDate.millis)
    parcel.writeString(type.toString())
    parcel.writeByte(if (skipWaterDay1) 1 else 0)
    parcel.writeByte(if (skipWaterDay2) 1 else 0)
    parcel.writeByte(if (skipWaterDay3) 1 else 0)
}

override fun describeContents(): Int = 0

companion object CREATOR : Parcelable.Creator<DietEntity> {
    override fun createFromParcel(parcel: Parcel): DietEntity = DietEntity(parcel)
    override fun newArray(size: Int): Array<DietEntity?> = arrayOfNulls(size)
}