0

In my class i try to pass ArrayList into service using put extra method and when i try to read data in my service my application crashes

This is my code:

private var listOfMyItems:ArrayList<MyItem> = arrayListOf()

var i = Intent(context, MyService::class.java)
i.putExtra("list", listOfMyItems)
context.bindService(i, getConnectionObject(), Context.BIND_AUTO_CREATE)
context.startService(i)

And this is how i try to get data in my service:

 override fun onBind(intent: Intent?): IBinder? {
        val a= intent?.getSerializableExtra("list") as ArrayList<MyItem>
        val b: ArrayList<MyItem> = a.toCollection(ArrayList())
     
        return null
    }

This is my data class:

data class MyItem(val location:Location, var time:Long = 0): Serializable

And i get susch exception: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object

There are my full logs:

    Process: com.example.gpstest, PID: 24374
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.gpstest/com.example.gpstest.MainActivity}: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.gpstest.MyItem)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6130)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.gpstest.MyItem)
        at android.os.Parcel.writeSerializable(Parcel.java:1535)
        at android.os.Parcel.writeValue(Parcel.java:1483)
        at android.os.Parcel.writeList(Parcel.java:820)
        at android.os.Parcel.writeValue(Parcel.java:1432)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:732)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
        at android.os.Bundle.writeToParcel(Bundle.java:1157)
        at android.os.Parcel.writeBundle(Parcel.java:772)
        at android.content.Intent.writeToParcel(Intent.java:8694)
        at android.app.ActivityManagerProxy.bindService(ActivityManagerNative.java:4397)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1464)
        at android.app.ContextImpl.bindService(ContextImpl.java:1423)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:636)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:636)
        at com.example.gpstest.GpsTracker.create(GpsHost.kt:41)
        at com.example.gpstest.MainActivity.onRequestPermissionsResult(MainActivity.kt:46)
        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7125)
        at android.app.Activity.dispatchActivityResult(Activity.java:6977)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
        at android.app.ActivityThread.-wrap20(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6130) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
     Caused by: java.io.NotSerializableException: android.location.Location
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
        at android.os.Parcel.writeSerializable(Parcel.java:1530)
        at android.os.Parcel.writeValue(Parcel.java:1483) 
        at android.os.Parcel.writeList(Parcel.java:820) 
        at android.os.Parcel.writeValue(Parcel.java:1432) 
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:732) 
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
        at android.os.Bundle.writeToParcel(Bundle.java:1157) 
        at android.os.Parcel.writeBundle(Parcel.java:772) 
        at android.content.Intent.writeToParcel(Intent.java:8694) 
        at android.app.ActivityManagerProxy.bindService(ActivityManagerNative.java:4397) 
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1464) 
        at android.app.ContextImpl.bindService(ContextImpl.java:1423) 
        at android.content.ContextWrapper.bindService(ContextWrapper.java:636) 
        at android.content.ContextWrapper.bindService(ContextWrapper.java:636) 
        at com.example.gpstest.GpsTracker.create(GpsHost.kt:41) 
        at com.example.gpstest.MainActivity.onRequestPermissionsResult(MainActivity.kt:46) 
        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7125) 
        at android.app.Activity.dispatchActivityResult(Activity.java:6977) 
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4085) 
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
        at android.app.ActivityThread.-wrap20(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6130) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) ```

  • 1
    The error says `MyItem` can't be serialized, do you know why? Can you edit your post to include the full console log please? – aSemy Sep 01 '21 at 06:30
  • I have edited post, now can u say what im doing wrong? – Paweł Mańczak Sep 01 '21 at 06:54
  • Thanks, that gives some useful information. The root cause is `Caused by: java.io.NotSerializableException: android.location.Location`. When I searched for that error, I found out that Android uses `Parcelable`, not Java's `Serializable`. Do the answers here help you? https://stackoverflow.com/q/3323074/4161471 – aSemy Sep 01 '21 at 08:39

0 Answers0