Android活动恢复

时间:2018-01-26 11:07:05

标签: android android-fragments exception kotlin onresume

我有一个活动可以加载片段 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 Play统计数据),我只能在手机上看一次。我还没有使用该应用程序几个小时。我点击了应用程序图标,然后就抛出了异常。

目前的例子中有什么不对吗?我愿意接受如何修复错误或如何一致地重现错误的建议。 谢谢!

编辑: 我忘了分享DietEntity课程:https://pastebin.com/EkT79ubT 它位于pastebin中,因为它不允许我将大量代码放在包含大量文本的帖子中。

1 个答案:

答案 0 :(得分:1)

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