Caused by: java.lang.RuntimeException: Parcelable 在读取 Serializable 对象时遇到 IOException

Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object

我有一个 activity 和一个没有默认构造函数的演示者,注入了 koin:

class HomeActivityPresenter<V : HomeActivityNav>(dataManager: DataManager, schedulerProvider: SchedulerProvider) :
        BaseViewModel<V>(dataManager, schedulerProvider) HomeActivityListener, Serializable { 

在 activity 中,我使用 addFragment 方法膨胀片段:

fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int) {
    supportFragmentManager.inTransaction { add(frameId, fragment) }
}

我正在使用 Don't keep activities 测试我的应用程序。当 activity 打开时,我按下主页按钮将我的应用程序置于后台。当 activity 恢复时我遇到了这个崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfopackagename.ui.features.home.activity.HomeActivity}: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = packagename.ui.features.home.activity.HomeActivityPresenter)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = packagename.ui.features.home.activity.HomeActivityPresenter)
    at android.os.Parcel.readSerializable(Parcel.java:3136)
    at android.os.Parcel.readValue(Parcel.java:2917)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3244)
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
    at android.os.BaseBundle.unparcel(BaseBundle.java:236)
    at android.os.BaseBundle.getString(BaseBundle.java:1160)
    at android.os.BaseBundle.getString(BaseBundle.java:1182)
    at packagename.ui.features.orderstatus.PickupOrderStatusDetailFragment.onCreate(PickupOrderStatusDetailFragment.kt:49)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
    at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607)
    at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235)
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316)
    at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
    at packagename.features.home.activity.HomeActivity.onCreate(HomeActivity.kt:84)
    at android.app.Activity.performCreate(Activity.java:7802)
    at android.app.Activity.performCreate(Activity.java:7791)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
 Caused by: java.io.InvalidClassException: packagename.ui.features.home.activity.HomeActivityPresenter; no valid constructor
    at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:154)
    at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:798)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1873)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1412)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
    at android.os.Parcel.readSerializable(Parcel.java:3134)
    at android.os.Parcel.readValue(Parcel.java:2917) 
    at android.os.Parcel.readArrayMapInternal(Parcel.java:3244) 
    at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292) 
    at android.os.BaseBundle.unparcel(BaseBundle.java:236) 
    at android.os.BaseBundle.getString(BaseBundle.java:1160) 
    at android.os.BaseBundle.getString(BaseBundle.java:1182) 
    at packagename.ui.features.orderstatus.PickupOrderStatusDetailFragment.onCreate(PickupOrderStatusDetailFragment.kt:49) 
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586) 
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838) 
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659) 
    at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607) 
    at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235) 
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316) 
    at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106) 
    at packagename.ui.features.home.activity.HomeActivity.onCreate(HomeActivity.kt:84) 
    at android.app.Activity.performCreate(Activity.java:7802) 
    at android.app.Activity.performCreate(Activity.java:7791) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

这是我的崩溃指向的片段中的第 49 行:

   val orderStatus = args.getString(Constants.pickUpOrderCreate, "")

我不知道如何解决这个问题。

顺便说一句:为什么需要序列化视图模型。你想达到什么目的。当 activity 由于配置更改以外的任何原因被销毁时,视图模型不会以任何方式帮助您。它们将在配置更改后继续存在。