Search code examples
javaandroidsqliteserializable

Not Serializable SQLiteCursor when I go home


My app is a RSS adapter who works with SQlite, viewpagers and RecyclerViews. Everything works fine but once I leave my app pressing home button, my app crashes and shows the following logcat:

FATAL EXCEPTION: main
                                                                                        Process: com.herprogramacin.hermosaprogramacion, PID: 14900
                                                                                        java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.herprogramacin.hermosaprogramacion.UI.Adapters.RVAdapter)
                                                                                            at android.os.Parcel.writeSerializable(Parcel.java:1468)
                                                                                            at android.os.Parcel.writeValue(Parcel.java:1416)
                                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
                                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
                                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1079)
                                                                                            at android.os.Parcel.writeBundle(Parcel.java:711)
                                                                                            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
                                                                                            at android.os.Parcel.writeTypedArray(Parcel.java:1254)
                                                                                            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)
                                                                                            at android.os.Parcel.writeParcelable(Parcel.java:1437)
                                                                                            at android.os.Parcel.writeValue(Parcel.java:1343)
                                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
                                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
                                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1079)
                                                                                            at android.os.Parcel.writeBundle(Parcel.java:711)
                                                                                            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3701)
                                                                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4622)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                         Caused by: java.io.NotSerializableException: android.database.sqlite.SQLiteCursor
                                                                                            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
                                                                                            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
                                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
                                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
                                                                                            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
                                                                                            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
                                                                                            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
                                                                                            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
                                                                                            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
                                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
                                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
                                                                                            at android.os.Parcel.writeSerializable(Parcel.java:1463)
                                                                                            at android.os.Parcel.writeValue(Parcel.java:1416) 
                                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:686) 
                                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) 
                                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1079) 
                                                                                            at android.os.Parcel.writeBundle(Parcel.java:711) 
                                                                                            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137) 
                                                                                            at android.os.Parcel.writeTypedArray(Parcel.java:1254) 
                                                                                            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384) 
                                                                                            at android.os.Parcel.writeParcelable(Parcel.java:1437) 
                                                                                            at android.os.Parcel.writeValue(Parcel.java:1343) 
                                                                                            at android.os.Parcel.writeArrayMapInternal(Parcel.java:686) 
                                                                                            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) 
                                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1079) 
                                                                                            at android.os.Parcel.writeBundle(Parcel.java:711) 
                                                                                            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3701) 
                                                                                            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:4622) 
                                                                                            at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                            at android.os.Looper.loop(Looper.java:158) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Where we can read "Caused by: java.io.NotSerializableException:android.database.sqlite.SQLiteCursor"

Why Is it happening this?

EDIT 1:

How my app works.

  1. Main activity gets the XML code for RSS and once it finishes, MainActivity get the cursor and generates the Recycler View Adapter.

  2. Main activity creates the viewpager and sends to him the RecyclerView Adapter.

  3. ViewPager creates a new fragment and passes the adapter to the new Fragment

  4. The new fragment generates the view and sets the adapter with the data that ViewPager sent to him.

I realised that is crazy to generate the adapter in MainActivity and pass it to the fragment through 3 clases. Could be this the problem?


Solution

  • It sounds like you are holding a reference to a SQLiteCursor in an activity or other view logic. When Android is attempting to pause your activity it tries to serialize the data the activity currently has (and dependent objects) so that they can be deserialized when the activity resumes. Look around for how you are using SQLiteCursor, you should not be passing them around to your activity or any model objects used by your activity.