Parcelable encountered IOException writing serializable object when I try to open a new Activity from a Fragment through a Callback

I have the following requirement – PlannerAdapter must have a btn_dots which opens an AlertDialogFragment() popup, and when you tap on the Duplicate Planner button, I want to open the activity DuplicatePlanner with the planner’s title passed into it. To achieve this I am using two listeners interfaces onDupeListener and onDupeListenerListener

interface OnDupeListener: Serializable{
    fun onDupeClicked(title: String, onDupeListenerListener: OnDupeListenerListener)
}

interface OnDupeListenerListener: Serializable{
    fun onDupeClickedForReal(title: String)
}

Here’s the adapter:

PlannerAdapter(private val plannerList: RealmResults<Planner>, private val onDupeListenerListener: OnDupeListenerListener):
    RecyclerView.Adapter<PlannerAdapter.RecipeHolder>(),OnDupeListener,OnDeletePlannerListener,Serializable {

...
 holder.bindRow(itemRow, position, this, this, onDupeListenerListener)
...
}



class Holder(var v: View): RecyclerView.ViewHolder(v){
    var planner = Planner()

    fun bindRow(planner: AKPlanner, position: Int, dupeListener: OnDupeListener, deletePlannerListener: OnDeletePlannerListener, onDupeListenerListener:OnDupeListenerListener) {
        akplanner = planner
        
        v.btn_dots.setOnClickListener{
            val dialogFragment = AlertDialogFragment()
            val activity = v.context as FragmentActivity
            val fm: FragmentManager = activity.supportFragmentManager
            val args = Bundle()
            args.putSerializable("dupeListener", dupeListener)
            args.putSerializable("deleteListener",deletePlannerListener)
            args.putSerializable("dupeListenerListener",onDupeListenerListener)
            args.putSerializable("title",planner.title)
            dialogFragment.arguments = args
            dialogFragment.show(fm, AlertDialogFragment::class.java.simpleName)
        }
    }
}

override fun onDupeClicked(title: String, onDupeListenerListener: OnDupeListenerListener) {
   onDupeListenerListener.onDupeClickedForReal(title)
}

The AlertDialogFragment:

class AlertDialogFragment: DialogFragment() {
...
 val mArgs = arguments
        val dupeListener = mArgs?.getSerializable("dupeListener") as? OnDupeListener
        val deleteListener = mArgs?.getSerializable("deleteListener") as? OnDeletePlannerListener
        val dupeListenerListener = mArgs?.getSerializable("dupeListenerListener") as? OnDupeListenerListener
        val title = mArgs?.getString("title")

        btn_dupe_planner.setOnClickListener {
            dupeListener?.onDupeClicked(title!!, dupeListenerListener!!)
        }
      }
    }

Finally the PlannerFragment:

class PlannersFragment : Fragment(),OnDupeListenerListener {
...
 rv_planners.adapter = PlannerAdapter(planners,this)
...
    override fun onDupeClickedForReal(title: String) {
        val intent = Intent(requireContext(), DupePlannerActivity::class.java)
        intent.putExtra("title",title)
        requireContext().startActivity(intent)
    }

The full stack trace is:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.annabelkarmel.UI.Planners.AKPlannersColorAdapter)
    at android.os.Parcel.writeSerializable(Parcel.java:1833)
    at android.os.Parcel.writeValue(Parcel.java:1780)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:928)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1589)
    at android.os.Bundle.writeToParcel(Bundle.java:1253)
    at android.os.Parcel.writeBundle(Parcel.java:997)
    at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:125)
    at android.os.Parcel.writeTypedObject(Parcel.java:1634)
    at android.os.Parcel.writeTypedList(Parcel.java:1513)
    at android.os.Parcel.writeTypedList(Parcel.java:1470)
    at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51)
    at android.os.Parcel.writeParcelable(Parcel.java:1801)
    at android.os.Parcel.writeValue(Parcel.java:1707)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:928)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1589)
    at android.os.Bundle.writeToParcel(Bundle.java:1253)
    at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4545)
    at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7711)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 Caused by: java.io.NotSerializableException: android.widget.LinearLayout
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1240)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1789)
    at java.util.HashMap.writeObject(HashMap.java:1366)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1036)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1552)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1604)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1565)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1604)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1565)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
    at android.os.Parcel.writeSerializable(Parcel.java:1828)
    at android.os.Parcel.writeValue(Parcel.java:1780) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:928) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1589) 
    at android.os.Bundle.writeToParcel(Bundle.java:1253) 
    at android.os.Parcel.writeBundle(Parcel.java:997) 
    at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:125) 
    at android.os.Parcel.writeTypedObject(Parcel.java:1634) 
    at android.os.Parcel.writeTypedList(Parcel.java:1513) 
    at android.os.Parcel.writeTypedList(Parcel.java:1470) 
    at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51) 
    at android.os.Parcel.writeParcelable(Parcel.java:1801) 
    at android.os.Parcel.writeValue(Parcel.java:1707) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:928) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1589) 
    at android.os.Bundle.writeToParcel(Bundle.java:1253) 
    at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4545) 
    at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145) 
    at android.os.Handler.handleCallback(Handler.java:883) 
    at android.os.Handler.dispatchMessage(Handler.java:100) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7711) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

While waiting for an answer I will try to rewrite this logic using Android’s Navigation library

Author Details

admin photo

sobuj033@gmail.com

Sobuj Ali

Founder & CEO

Sobuj Ali is Founder & CEO at Apkpm.com. I have a good knowledge of Marketing. So I provide all Technology information on their website.

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Rating: