Android:退出在 onSave/onRecall 中序列化对象的 Activity 时出现致命异常 (NotSerializedException)

标签 android exception serialization

我正在开发一个使用一些自定义类作为成员的应用程序。 我已经在 onSaveInstanceState 和 onRestoreInstanceState 方法中实现了序列化/反序列化。

旋转屏幕时一切正常(应用程序被销毁并重新创建)并且我的对象已恢复。 但是,当按下主页按钮时,应用程序在 onPause 方法中崩溃(我没有修改)...

有任何关于发生了什么的线索吗?

这是重现该问题的代码示例

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();
    }
}

这是堆栈跟踪的开头

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)

...

最佳答案

你有3个选择:

  1. 使外部类也可序列化。

  2. 使可序列化的内部类静态。

  3. 创建另一个类并使该类可序列化(不要序列化内部类)。

第一个实际上是不鼓励的。

正如 Mayra 提到的,如果你实现 Parcelable,它会运行得更快。

关于Android:退出在 onSave/onRecall 中序列化对象的 Activity 时出现致命异常 (NotSerializedException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4598404/

相关文章:

c++ - 读写类对象到二进制文件

Android - sendBroadcast() 和 onReceive() - 进程间通信的同步解决方案

java - 什么操作会触发 ListView 中的 onItemSelected?

java - 如何替换数组适配器字符串中的特殊字符?

java - Jedis - 何时使用 returnBrokenResource()

c# - 当使用自定义契约(Contract)解析器而不是 JsonConverter 属性时,自定义 JsonConverter 被忽略以进行反序列化

jquery - hack jQuery 序列化不在表单内的 &lt;input&gt; 和 <select> 元素的值

android - 如何在 Android 图表的右侧绘制 Y 轴标签

c# - 处理sql异常的策略应该是什么?

java - 它们是异常而不是 Java 中的编译错误。为什么?