ObjectInputStream.readObject() 抛出的 NotSerializableException [英] NotSerializableException threw by ObjectInputStream.readObject()

查看:61
本文介绍了ObjectInputStream.readObject() 抛出的 NotSerializableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Bundle 在活动上传递 ArrayList(包含大图片)时遇到了一些问题.我失败了,因为 Bundle 有 1MB 的限制.通过一些研究,我发现序列化对象并将其保存到文件会更好,所以我创建了这个类:

I had some problems passing a ArrayList<People> (which contains large pictures) over Activities using Bundle. I failed because Bundle has a 1MB limit. With some research, i found out that it would be better Serializing the object and saving it to a file, so I made this class:

public class BundlePeople {

    public static long packPeople(Context context, List<People> people) {
        long timeToken = System.currentTimeMillis();
        String fileName = "temp_" + Long.toString(timeToken);
        try {
            FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(people);
            os.close();
            fos.close();
        } catch (IOException e) {
            Log.e("BundlePeople", e.getMessage());
        }

        return timeToken;
    }

    public static List<People> unpackPeople(Context context, long id) {
        List<People> list = new ArrayList<>();

        String fileName = "temp_" + Long.toString(id);
        try {
            FileInputStream fis = context.openFileInput(fileName);
            ObjectInputStream is = new ObjectInputStream(fis);
            list.addAll((List<People>) is.readObject()); //Here I get NotSerializableException on second call
            is.close();
            fis.close();
        } catch (IOException | ClassNotFoundException e) {
            Log.e("BundlePeople", e.getMessage());
        }

        return list;
    }
}

它工作一次,但是当我再次尝试使用它时,它抛出 NotSerializableExcpetion

It works once, but when I try use it again, it throws NotSerializableExcpetion

第一次调用:

传递(FirstActivity.java)

Bundle bundle = new Bundle();
bundle.putLong("peopleFileId", BundlePeople.packPeople(FirstActivity.this, listPeople));
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtras(bundle);    
startActivityForResult(i.setFlags(SecondActivity.FLAG_SELECT), 0);

接收(SecondActivity.java)

long id = getIntent().getExtras().getLong("peopleFileId");
list = new ArrayList<>();
list.addAll(BundlePeople.unpackPeople(getApplicationContext(), id));

目前还不错,第二次使用时出现问题:

It works fine for now, the problem is when I use it for the second time:

第二次通话:

传递(SecondActivity.java)

Intent i = new Intent();
Bundle b = new Bundle();
b.putLong("peopleFileId", BundlePeople.packPeople(SecondActivity.this, list));
i.putExtras(b);
setResult(REQUEST_PEOPLE, i);
finish();

接收(FirstActivity.java [onActivityResult()])

List<People> list = BundlePeople.unpackPeople(
                        getApplicationContext(), data.getExtras().getLong("peopleFileId"));

这里我的列表是 null 因为在 BundlePeople 上的第一个代码指示的行上抛出异常.

Here my list is null because an exception is thrown on BundlePeople at the line indicated on first code.

异常信息:读取异常;java.io.NotSerializableException: br.com.something.activities.SecondActivity$1

我的问题是,为什么我会收到此异常?这是通过活动传递大对象的更好方法吗?如果是,这里有什么问题?

My questions is, why am I getting this exception? Is this the better way to pass large objects over activities? If it is, what's wrong here?

编辑

People.java

public class People implements Serializable {

    private static final long serialVersionUID = -9171517741542003990L;

    private Integer id;
    private String name;
    private BitmapSerializable image;

    public BitmapSerializable getImage() {
        return image;
    }

    public void setImage(BitmapSerializable image) {
        this.image = image;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

推荐答案

您:

  • 序列化一个不可序列化的对象;
  • 忽略异常;
  • 保存了文件;和
  • 尝试对其进行反序列化.

幸运的是Java在这种情况下将异常写入文件并在反序列化时抛出它.异常消息为您提供了相关类的名称.

Fortunately Java writes the exception to the file in this circumstance and threw it when you deserialized. The exception message gives you the name of the class concerned.

但它不应该走到这一步.

But it should never have got this far.

这篇关于ObjectInputStream.readObject() 抛出的 NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆