我是Java和Android的初学者,我得到如下:
“java.io.streamcorruptedexception:格式错误:0”
当我试图读回那个物体时。
但我只在棒棒糖和棉花糖设备上得到它,当我在牛轧糖(7.1)上运行这个程序时,我一点问题都没有。
有什么建议吗?

public class MainActivity extends AppCompatActivity {

public final String FILE_NAME = "FILE_NAME";
private List<Item>;
private ReadWriteFunctions readWriteFunctions = new ReadWriteFunctions();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Bitmap defaultBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.no_pic);

    //Read from internal storage
    items =readWriteFunctions.readObj(this,FILE_NAME);
    if (items == null){//if the file don't exist it returns null
        items = new ArrayList<>();
        items.add(new Item(false,"",defaultBitmap));
        Log.d("readObj", "Created new list");
    }

类代码:
public class ReadWriteFunctions {


public ArrayList<Item> readObj(Context context, String fileName) {
    ArrayList<Item> arrayList;
    try {
        FileInputStream fis = context.openFileInput(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        arrayList = (ArrayList<Item>) ois.readObject();
        ois.close();
        return arrayList;
    }catch (IOException | ClassNotFoundException e){
        e.printStackTrace();
        return null;
    }
}

public void writObj(Context context,String fileName,Object obj) {
    try {
        FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.close();
        Log.d("writObj", "Saved");
    }catch (IOException e){
        e.printStackTrace();
        Log.d("writObj", "Save Fault");
    }

}

}
公共类项实现可序列化{
private boolean itemChecked;
private String itemText;
private transient Bitmap itemPicture;


public Item(Item item) {//Constructor that receive item
    this.setItemChecked(item.isItemChecked());
    this.setItemText(item.getItemText());
    this.setItemPicture(item.getItemPicture());

}

public Item(boolean itemChecked, String itemText, Bitmap itemPicture) {
    this.itemChecked = itemChecked;
    this.itemText = itemText;
    this.itemPicture = itemPicture;
}


public boolean isItemChecked() {
    return itemChecked;
}

public void setItemChecked(boolean itemChecked) {
    this.itemChecked = itemChecked;
}

public String getItemText() {
    return itemText;
}

public void setItemText(String itemText) {
    this.itemText = itemText;
}

public Bitmap getItemPicture() {
    return itemPicture;
}

public void setItemPicture(Bitmap itemPicture) {
    this.itemPicture = itemPicture;
}



private void writeObject(java.io.ObjectOutputStream out) throws IOException {

    itemPicture.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.defaultWriteObject();

}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

    itemPicture = BitmapFactory.decodeStream(in);
    in.defaultReadObject();

}

}

最佳答案

只是把我解决问题的方法发布给以后遇到同样问题的人。
在:

public class Item implements Serializable

我改了:这行:
private boolean itemChecked;

到这一行:
private Boolean itemChecked;

因为我知道布尔型的原语是可序列化的,所以我不知道它为什么会有不同,我也不明白为什么它对牛轧糖起作用,而对棒棒糖和棉花糖不起作用。
但现在效果不错。
如果有人知道为什么,我很高兴听到。

09-07 11:04