-1

我将模型列表从一个片段 A 传递到另一个片段 B,当我按下片段 B 上的最近应用程序按钮时,它崩溃了。

data class CategoryModel(

@SerializedName("id")
val id : Int,

@SerializedName("name")
val categoryName : String,

var isSelected : Boolean,

@SerializedName("image")
val imageUrl : String,

var isOpened: Boolean = false,

@SerializedName("service")
val services : MutableList<ServiceModel>,

var selectedServiceListAdapter: SelectedSeerviceListRecyclerAdapter? = null) : Serializable

这是片段 A :

清单是

val localCategoryList: MutableList<CategoryModel> = arrayListOf()

我正在通过以下列表:

val argument = bundleOf("list" to localCategoryList)

findNavController(this).navigate(R.id.action_categoryFragment_to_serviceListFragment, argument)

在片段 B 中:

categoryList = arguments?.get("list") as MutableList<CategoryModel>

下面是堆栈跟踪

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.xxx.xxx.xxx.model.CategoryModel)
    at android.os.Parcel.writeSerializable(Parcel.java:1394)
    at android.os.Bundle.writeToParcel(Bundle.java:1034)
    at android.os.Parcel.writeBundle(Parcel.java:669)
    at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:167)
    at android.os.Parcel.writeTypedList(Parcel.java:1112)
    at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51)
 androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51)

    at android.os.Parcel.writeBundle(Parcel.java:669)
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3270)

    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)


Caused by: java.io.NotSerializableException: com.xxxx.xxxx.xxxx.view.activities.ProviderDetailsActivity
    at 

   java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at android.os.Parcel.writeSerializable(Parcel.java:1389)
    at android.os.Parcel.writeValue(Parcel.java:1341) 

    at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:169) 
    at android.os.Parcel.writeTypedList(Parcel.java:1112) 
    at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51) 
    at android.os.Parcel.writeParcelable(Parcel.java:1363) 
    at android.os.Parcel.writeValue(Parcel.java:1268) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:644) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) 
    at android.os.Bundle.writeToParcel(Bundle.java:1034) 
    at android.os.Parcel.writeBundle(Parcel.java:669) 
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3270) 
    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4385) 
    at android.os.Handler.handleCallback(Handler.java:739) 

    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
4

1 回答 1

2

您需要像下面的示例一样打包数据类

Parcelize
data class WebViewTemplateParam(
    var type:FeedsType?,
    var placeholder: String = "",
    var message: String = "",
    var title: String? = "",
    var image: Int?= null):Parcelable
于 2019-08-21T12:41:15.477 回答