Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotSerializableException when pressing home button

Tags:

android

I have an Activity in which I have a private class Data. In the onSaveInstanceState method, I try to save this an instance of Data. This is my whole Activity:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onSaveInstanceState(Bundle outState){
        outState.putSerializable("TEST", new Data());
    }

    private class Data implements Serializable {

        private static final long serialVersionUID = -4312723541994925110L;

    }
}

Now when I change the orientation of the device, the Data object is saved and read as it should. However, when I press the home button, the app crashes with the following in the logcat:

05-10 20:05:51.895: E/AndroidRuntime(30317): FATAL EXCEPTION: main 05-10 20:05:51.895: E/AndroidRuntime(30317): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.rigidbits.test.TestActivity$Data) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Parcel.writeSerializable(Parcel.java:1176) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Parcel.writeValue(Parcel.java:1130) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Parcel.writeMapInternal(Parcel.java:488) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Bundle.writeToParcel(Bundle.java:1552) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Parcel.writeBundle(Parcel.java:502) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1615) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2298) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.app.ActivityThread.access$1700(ActivityThread.java:117) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Handler.dispatchMessage(Handler.java:99) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Looper.loop(Looper.java:130) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.app.ActivityThread.main(ActivityThread.java:3683) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.lang.reflect.Method.invokeNative(Native Method) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.lang.reflect.Method.invoke(Method.java:507) 05-10 20:05:51.895: E/AndroidRuntime(30317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 05-10 20:05:51.895: E/AndroidRuntime(30317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 05-10 20:05:51.895: E/AndroidRuntime(30317): at dalvik.system.NativeStart.main(Native Method) 05-10 20:05:51.895: E/AndroidRuntime(30317): Caused by: java.io.NotSerializableException: com.rigidbits.test.TestActivity 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689) 05-10 20:05:51.895: E/AndroidRuntime(30317): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653) 05-10 20:05:51.895: E/AndroidRuntime(30317): at android.os.Parcel.writeSerializable(Parcel.java:1171) 05-10 20:05:51.895: E/AndroidRuntime(30317): ... 16 more

When I comment the line in the onSaveInstanceState method so the Data object is not saved, the app disappears correctly.

Any help with this?

like image 475
nhaarman Avatar asked May 10 '12 14:05

nhaarman


1 Answers

Ok I believe the problem is that you are using a private inner class.

Therefore it has access to the methods and fields of your outer class i.e. your activity.

Because your activity isn't serializble (and so it shouldn't be), your getting your exception.

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

enter image description here

There are two solutions.

  • Make your inner class static

  • Move your inner class to be a class of its own and declare it public

These solutions both make your Data class a class in its own right and then it doesn't need your activity instance to exist

Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

like image 109
Blundell Avatar answered Oct 25 '22 22:10

Blundell