gpt4 book ai didi

java - ObjectInputStream.readObject() 抛出 NotSerializedException

转载 作者:行者123 更新时间:2023-12-01 11:35:29 25 4
gpt4 key购买 nike

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

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

第一次通话:

传递(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));

目前工作正常,问题是当我第二次使用它时:

第二次通话:

传递(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 上抛出异常在第一个代码指示的行处。

异常消息:Read an exception; java.io.NotSerializableException: br.com.something.activities.SecondActivity$1

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

编辑

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 将异常写入文件,并在反序列化时抛出它。异常消息为您提供相关类的名称。

但它不应该走到这一步。

关于java - ObjectInputStream.readObject() 抛出 NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com