2

The issue I am facing is, when I press home button while application is in foreground, then application crashes by throwing AppCompat not serializable exception. Here is the stacktrace of exception. Please suggest the solution. I am not using any Custom data type which is not Serializable. This is android view which is causing problem

FATAL EXCEPTION: main
                                                  java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.test.XYZFragment)
                                                        at android.os.Parcel.writeSerializable(Parcel.java:1285)
                                                        at android.os.Parcel.writeValue(Parcel.java:1233)
                                                        at android.os.Parcel.writeMapInternal(Parcel.java:591)
                                                        at android.os.Bundle.writeToParcel(Bundle.java:1646)
                                                        at android.os.Parcel.writeBundle(Parcel.java:605)
                                                        at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
                                                        at android.os.Parcel.writeTypedArray(Parcel.java:1102)
                                                        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:385)
                                                        at android.os.Parcel.writeParcelable(Parcel.java:1254)
                                                        at android.os.Parcel.writeValue(Parcel.java:1173)
                                                        at android.os.Parcel.writeMapInternal(Parcel.java:591)
                                                        at android.os.Bundle.writeToParcel(Bundle.java:1646)
                                                        at android.os.Parcel.writeBundle(Parcel.java:605)
                                                        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2613)
                                                        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3232)
                                                        at android.os.Handler.handleCallback(Handler.java:730)
                                                        at android.os.Handler.dispatchMessage(Handler.java:92)
                                                        at android.os.Looper.loop(Looper.java:176)
                                                        at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                        at java.lang.reflect.Method.invoke(Method.java:525)
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                        at dalvik.system.NativeStart.main(Native Method)
                                                     Caused by: java.io.NotSerializableException: android.support.v7.widget.AppCompatAutoCompleteTextView
                                                        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
                                                        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
                                                        at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
                                                        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
                                                        at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
                                                        at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
                                                        at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
                                                        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
                                                        at android.os.Parcel.writeSerializable(Parcel.java:1280)
                                                        at android.os.Parcel.writeValue(Parcel.java:1233) 
                                                        at android.os.Parcel.writeMapInternal(Parcel.java:591) 
                                                        at android.os.Bundle.writeToParcel(Bundle.java:1646) 
                                                        at android.os.Parcel.writeBundle(Parcel.java:605) 
                                                        at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137) 
                                                        at android.os.Parcel.writeTypedArray(Parcel.java:1102) 
                                                        at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:385) 
                                                        at android.os.Parcel.writeParcelable(Parcel.java:1254) 
                                                        at android.os.Parcel.writeValue(Parcel.java:1173) 
                                                        at android.os.Parcel.writeMapInternal(Parcel.java:591) 
                                                        at android.os.Bundle.writeToParcel(Bundle.java:1646) 
                                                        at android.os.Parcel.writeBundle(Parcel.java:605) 
                                                        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2613) 
                                                        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3232) 
                                                        at android.os.Handler.handleCallback(Handler.java:730) 
                                                        at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                        at android.os.Looper.loop(Looper.java:176) 
                                                        at android.app.ActivityThread.main(ActivityThread.java:5419) 
                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                        at java.lang.reflect.Method.invoke(Method.java:525) 
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
                                                        at dalvik.system.NativeStart.main(Native Method) 
vijaypalod
  • 398
  • 4
  • 15
  • Can you post a block of code ? – AndroidGorilllla Nov 28 '16 at 07:43
  • can you post ur code in stop,onpause and ondestroy methods – Prashanth Debbadwar Nov 28 '16 at 07:46
  • I would check on the instance you are trying to serialize (with Parcelable)(the instance is the activity and his instance) since you have `java.io.NotSerializableException`. – AxelH Nov 28 '16 at 07:53
  • Possible duplicate of [Parcelable encountered IOException writing serializable object getactivity()](http://stackoverflow.com/questions/23142893/parcelable-encountered-ioexception-writing-serializable-object-getactivity) – AxelH Nov 28 '16 at 07:57

3 Answers3

4

I have searched a bit and found the solution that while moving to the background, by default it was calling method onSaveInstanceState for saving my fragment's state. What I did is just override that method and commented the calling of super method so that it would not save app state and thus no serializable object is needed.

@Override
public void onSaveInstanceState(Bundle outState) {
  //Do not call super class method here.
  //super.onSaveInstanceState(outState);
}
vijaypalod
  • 398
  • 4
  • 15
1

check this Causedby:java.io.NotSerializableException: android.support.v7.widget.AppCompatAutoCompleteTextView and document

check you gradle whether you add the lib for AppCompat and more check here

Community
  • 1
  • 1
Raju
  • 1,183
  • 3
  • 11
  • 19
  • Like you have pointed out, this is a duplicated of the link you have add (I took the liberty to flag it). This should help him to find a solution. For the rest, your solution lake of precision, this is more like a comment for the moment – AxelH Nov 28 '16 at 07:59
1

It's a bit difficult to see what's going on without the source code, but it is clear to me that you are trying to save the state by putting some object as a Serializable into the Bundle in onSaveInstanceState. Unfortunately you are either trying to save an object that references an activity or an activity itself, which isn't going to work.

You can't put references to activities into the saved states as the activities are managed externally to your app. Even if you somehow manage to save the activity's object, when a new activity is created and gets the saved state, the old activity is dead now and not managed by the system, so it's useless to you.

How you can fix the code depends on what state you are trying to save. If you need to save the state inside the activity, you need to save only that state, separated from the rest of the object.

Malcolm
  • 41,014
  • 11
  • 68
  • 91