2

Saving the data works no problem. But when loading the data I get this error message

A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa3907e44 in tid 2407 (myapplication)

This method saves the data using googles GSON library. The class is part of an adapter and is called every time the user presses a button in a dialog.

 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;
    }

This method loads the data using googles GSON library. This method also causes the app to crash giving the error see above

    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;
        }
HaloMediaz
  • 1,251
  • 2
  • 13
  • 31

1 Answers1

0

I had the same issue as you. It took a couple minutes before it dawned on me: I was asking Gson to convert the JSON into a list of abstract objects. As you can't instantiate abstract classes that of course won't work, though it was a bit surprising to see the SIGSEGV instead of a better exception.

Is UserData an abstract class? In that case you either have to change to using another class or maybe the solution described in https://stackoverflow.com/a/9106351/467650 .

Community
  • 1
  • 1
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76