저장된 메모를 읽으려고 할 때 다음과 같은 오류가 발생한다는 응용 프로그램 코드를 메모하는 메모가 있습니다.
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;
}
}
이는 serialVersionUID
Note pojo에 추가하지 않기 때문 입니다.
구현하는 각 클래스 Serializable
는 추가해야합니다.serialVersionUID
메모를 다음으로 변경할 수 있습니다.
public class Note implements Serializable {
private static final long serialVersionUID = 1234567L;
...
...
}
그러나 데이터가 변경 될 때마다 이전 데이터와 호환되지 않는 변경이있을 수 있으므로 데이터 지속성을 위해 Serializable에 의존 해서는 안됩니다. Serializable 객체는 내 생각에 임시 사용을 위해 사용해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다