2
保存數據沒有問題。但是當加載數據時,我得到這個錯誤信息從文件加載數據導致致命信號11
A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa3907e44 in tid 2407 (myapplication)
這個方法使用googles GSON庫保存數據。該類是適配器的一部分,每次用戶在對話框中按下按鈕時都會調用該類。
public int saveListToFile(UserData data, Context context) {
itemsData.add(data);
notifyItemInserted(itemsData.size()-1);
String filename = "colors";
File file = new File(context.getFilesDir(), filename);
try {
BufferedWriter buffWriter = new BufferedWriter(new FileWriter(file, true));
Gson gson = new Gson();
Type type = new TypeToken<List<UserData>>() {}.getType();
String json = gson.toJson(itemsData, type);
buffWriter.append(json);
buffWriter.newLine();
buffWriter.close();
} catch (IOException e) {
return -1;
}
return 0;
}
該方法使用Google谷歌GSON庫加載數據。此方法也導致應用程序崩潰給出錯誤見上面
public int readCurrentList() {
String filename = "colors";
File file = new File(getFilesDir(), filename);
try {
BufferedReader buffReader = new BufferedReader(new FileReader(file));
String line;
Gson gson = new Gson();
Type type = new TypeToken<List<UserData>>() {}.getType();
while ((line = buffReader.readLine()) != null) {
itemsData.addAll((java.util.Collection<? extends UserData>) gson.fromJson(line, type));
}
buffReader.close();
} catch (IOException e) {
return -1;
}
return 0;
}
你可以檢查你的應用程序附加調試器或visualvm或JMC記憶體消耗 – learningJava 2015-03-30 19:23:57
你可以發佈json字符串,你正在保存? – 2015-04-02 09:51:07
@ Pankaj Nimgade我保存位圖(android.graphics.Bitmap)對象。這可能是問題嗎? – HaloMediaz 2015-04-02 23:48:56