我有我的筆記應用程序代碼中,當我嘗試讀取保存的筆記它給我的錯誤是這樣的:InvalidClassException serialVersionUID的錯誤,而讀文件
java.io.InvalidClassException: com.slide.adarsh.ezswipe.Note; Incompatible class (SUID): com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =3563451862165282381L; but expected com.slide.adarsh.ezswipe.Note: static final long serialVersionUID =0L;
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2341)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at com.slide.adarsh.ezswipe.Utilities.getAllSavedNotes(Utilities.java:62)
12-04 18:58:21.854 18205-18205/com.slide.adarsh.ezswipe W/System.err: at com.slide.adarsh.ezswipe.NoteList.onResume(NoteList.java:32)
這裏是代碼:
Utilities.java
public class Utilities {
public static final String FILE_EXTENSION=".bin";
public static boolean saveNote(Context context,Note note) {
String fileName=String.valueOf(note.getDateTime())+FILE_EXTENSION;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos=context.openFileOutput(fileName,context.MODE_PRIVATE);
oos=new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context){
ArrayList<Note> not=new ArrayList<Note>();
File filesDir=context.getFilesDir();
ArrayList<String> noteFiles=new ArrayList<String>();
for (String file : filesDir.list()){
if (file.endsWith(FILE_EXTENSION)){
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for (int i=0;i<noteFiles.size();i++) {
try {
fis=context.openFileInput(noteFiles.get(i));
ois=new ObjectInputStream(fis);
not.add((Note) ois.readObject()); //this is the place where exception is raised
fis.close();
ois.close();
} catch (IOException|ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return not;
}
}
這裏是我的注意類它實現Serializable
:
public class Note implements Serializable {
private long mDateTime; //creation time of the note
private String mTitle; //title of the note
private String mContent; //content of the note
public Note(long dateInMillis, String title, String content) {
mDateTime = dateInMillis;
mTitle = title;
mContent = content;
}
public void setDateTime(long dateTime) {
mDateTime = dateTime;
}
public void setTitle(String title) {
mTitle = title;
}
public void setcontent(String content) {
mContent = content;
}
public long getDateTime() {
return mDateTime;
}
public String getDateTimeFormatted(Context context) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"
, context.getResources().getConfiguration().locale);
formatter.setTimeZone(TimeZone.getDefault());
return formatter.format(new Date(mDateTime));
}
public String getTitle() {
return mTitle;
}
public String getContent() {
return mContent;
}
}
可能的[java.io.invalidClassException序列化/反序列化過程]重複(http://stackoverflow.com/questions/6920352/java-io-invalidclassexception-during-serializing-deserializing) –
@ CharukaSliva我已經通過它,但沒有幫助,我甚至嘗試在Serializable類中設置serialversionUid,但它沒有奏效 – Adarsh
下面給出的所有答案都是完美的,但是你會遇到嚴重麻煩的人,以及代碼中的每個更改都會強制您使用舊的序列化對象等進行重新測試。你爲什麼不改變你的方法,並嘗試將數據保存在sharedPrefernces或sqlite或一些文件。 – nobalG