I have got a note taking application code in that when I try to read the saved notes it gives me error like this:
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)
here is the code:
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;
}
}
here is my Note class which implements 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;
}
}