开发者

Android: Fatal Exception (NotSerializableException) when quitting an activity that serializes objects in onSave/onRecall

开发者 https://www.devze.com 2023-02-02 04:53 出处:网络
I am developing an app that uses some custom classes as members. I have implemented serialization/de-serialization in the onSaveInstanceState and onRestoreInstanceState methods.

I am developing an app that uses some custom classes as members. I have implemented serialization/de-serialization in the onSaveInstanceState and onRestoreInstanceState methods.

It all works fine when rotating the screen (app is destroyed and re-created) and my objects are restored. However, when pressing the home button, the app crashes in the onPause method (which I have not modified)...

Any clue as to what's happening?

Here's a code sample that reproduces the issue

import java.io.Serializable;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class InvestigateError extends Activity {

    private static final String LOG_TAG = "MYERROR";
    protected ShowInfo myShowInfo;

    protected class ShowInfo implements Serializable
    {
        private static final long serialVersionUID = 1L开发者_开发知识库;
        public String title;
    }

    public void populateMembers()
    {
        myShowInfo = new ShowInfo();
        myShowInfo.title = "Was I serialized???";
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if(savedInstanceState == null)
            populateMembers();
        Log.d(LOG_TAG,"Activity created");

    }

    /* Save members */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(LOG_TAG,"__ SAVEINSTANCE __");
        outState.putSerializable("myshowInfo", myShowInfo);
        super.onSaveInstanceState(outState);
        }

    /* Restore members */
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(LOG_TAG,"__ RECALLINSTANCE __");
        myShowInfo = (ShowInfo) savedInstanceState.getSerializable("myshowInfo");
        Log.d(LOG_TAG,"string from object: " + myShowInfo.title);
    }

    @Override
    protected void onDestroy() {
        Log.d(LOG_TAG,"__ DESTROY __");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        Log.d(LOG_TAG,"__ PAUSE __");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d(LOG_TAG,"__ STOP __");
        super.onStop();
    }
}

Here's the beginning of the stack trace

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.esquared.InvestigateError.InvestigateError$ShowInfo)

at android.os.Parcel.writeSerializable(Parcel.java:1160)

at ndroid.os.Parcel.writeValue(Parcel.java:1114)

at android.os.Parcel.writeMapInternal(Parcel.java:479)

...


You have 3 choices:

  1. Make the outer class serializable as well.

  2. Make your serializable inner class static.

  3. Create another class and make that class serializable (don't serialize an inner class).

First one is actually discouraged.

And as Mayra mentioned, it will work faster if you implement Parcelable.

0

精彩评论

暂无评论...
验证码 换一张
取 消