从文件加载数据导致致命信号11

问题描述:

保存数据没有问题。但是当加载数据时,我得到这个错误信息从文件加载数据导致致命信号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

我和你有同样的问题。花了几分钟才明白我的意思:我要Gson将JSON转换成抽象对象列表。因为你不能实例化那些当然不起作用的抽象类,尽管看到SIGSEGV而不是更好的异常有点令人惊讶。

UserData是抽象类吗?在这种情况下,您必须更改为使用其他课程,或者使用https://stackoverflow.com/a/9106351/467650中描述的解决方案。