2015-03-30 79 views
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; 
     } 
+0

你可以檢查你的應用程序附加調試器或visualvm或JMC記憶體消耗 – learningJava 2015-03-30 19:23:57

+0

你可以發佈json字符串,你正在保存? – 2015-04-02 09:51:07

+0

@ Pankaj Nimgade我保存位圖(android.graphics.Bitmap)對象。這可能是問題嗎? – HaloMediaz 2015-04-02 23:48:56

回答

0

我和你有同樣的問題。花了幾分鐘才明白我的意思:我要Gson將JSON轉換成抽象對象列表。因爲你不能實例化那些當然不起作用的抽象類,儘管看到SIGSEGV而不是更好的異常有點令人驚訝。

UserData是抽象類嗎?在這種情況下,您必須更改爲使用其他課程,或者使用https://stackoverflow.com/a/9106351/467650中描述的解決方案。

相關問題