I
I
Igor2015-07-14 19:46:02
Android
Igor, 2015-07-14 19:46:02

Why is NotSerializableException thrown?

There is a SessionDialogFragment class that I call in the activity. When you flip the screen, everything is OK, onSaveInstanceState passes then onCreate, the data in the Bundle is normally saved and retrieved, without errors. But when I press Home, an exception occurs:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.dialogs.SessionDialogFragment$DialogButton)
            at android.os.Parcel.writeSerializable(Parcel.java:1279)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeList(Parcel.java:622)
            at android.os.Parcel.writeValue(Parcel.java:1195)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1619)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.app.FragmentState.writeToParcel(Fragment.java:132)
            at android.os.Parcel.writeTypedArray(Parcel.java:1102)
            at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:365)
            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:1619)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2078)
            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:2874)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.io.NotSerializableException: com.example.dialogs.SessionDialogFragment
            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 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:1274)
            at android.os.Parcel.writeValue(Parcel.java:1233)
            at android.os.Parcel.writeList(Parcel.java:622)
            at android.os.Parcel.writeValue(Parcel.java:1195)
            at android.os.Parcel.writeMapInternal(Parcel.java:591)
            at android.os.Bundle.writeToParcel(Bundle.java:1619)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.app.FragmentState.writeToParcel(Fragment.java:132)
            at android.os.Parcel.writeTypedArray(Parcel.java:1102)
            at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:365)
            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:1619)
            at android.os.Parcel.writeBundle(Parcel.java:605)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2078)
            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:2874)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

public class SessionDialogFragment extends DialogFragment {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        inflater = getActivity().getLayoutInflater();

        if (savedInstanceState != null) {
            buttons = (ArrayList<DialogButton>) savedInstanceState.getSerializable("buttons");
            sessionItems = (ArrayList<SessionItem>) savedInstanceState.getSerializable("sessionItems");
            title = savedInstanceState.getString("title");
            showIndicator = savedInstanceState.getBoolean("showIndicator");
        }
    }

    class DialogButton implements Serializable {
        String text;
        int resId;
        ButtonClickListener clickListener;

        public DialogButton(String text, int resId, ButtonClickListener clickListener) {
            this.text = text;
            this.resId = resId;
            this.clickListener = clickListener;
        }

        public String getText() {
            return text;
        }

        public int getResId() {
            return resId;
        }

        public ButtonClickListener getClickListener() {
            return clickListener;
        }

    }

    public abstract class ButtonClickListener implements View.OnClickListener, Serializable {

        @Override
        public abstract void onClick(View view);

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putSerializable("buttons", buttons);
        outState.putSerializable("sessionItems", sessionItems);
        outState.putString("title", title);
        outState.putBoolean("showIndicator", showIndicator);

        super.onSaveInstanceState(outState);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-07-14
@Ewintory

Well, who, who does that! Why was it necessary to shove DialogButton with ButtonClickListener into a bundle? What were you thinking?) And what is your ButtonClickListener, since it is an abstract class and not an interface??

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question