0

I tried this so far: Why do I get an UnsupportedOperationException when trying to remove an element from a List? Well, The exception doesn't appear anymore but.. the app doesn't reply anymore.. I store 2 lists in my shared preferences with this method:

public void storeList(List<?> lList, String storeString) {
        String s = new Gson().toJson(lList, new TypeToken<List<?>>() {
        }.getType());
        editPrefsLists.putString(storeString, s).apply();
        editPrefsLists.commit();
    }

Well, before I get the UnsupportedOperationException I tried to read the list with this method:

public <T> List<T> stringToArray(String storedString, Class<T[]> clazz) {
        T[] arr = new Gson().fromJson(sharedPrefsLists.getString(storedString, null), clazz);
        if (arr != null) {
            return Arrays.asList(arr); //or return Arrays.asList(new Gson().fromJson(s, clazz)); for a one-liner
        } else {
            return null;
        }
    }

After the Exception I tried it with a LinkedList and make this:

public <T> List<T> stringToArray(String storedString, Class<T[]> clazz) {
        T[] arr = new Gson().fromJson(sharedPrefsLists.getString(storedString, null), clazz);
        if (arr != null) {
            LinkedList<T> newList = new LinkedList<>(Arrays.asList(arr));
            return newList; //or return Arrays.asList(new Gson().fromJson(s, clazz)); for a one-liner
        } else {
            return null;
        }
    }

Now, the app doesn't reply anymore and close itself. How can I solve this problem?

Community
  • 1
  • 1
blueman3399
  • 207
  • 3
  • 13
  • Can you post the error message from logcat? – George Mulligan Mar 31 '16 at 02:05
  • There is no error message.. The app just don't reply anymore. – blueman3399 Mar 31 '16 at 02:08
  • Odd that the app would close itself then. When you say it doesn't reply what does it do? Does it ever display any activity? Does it go back to showing your home screen after closing itself? – George Mulligan Mar 31 '16 at 02:10
  • There is a no data on the Activity and after a few seconds appears a dialog with "The app doesn't reply. Close or wait".. – blueman3399 Mar 31 '16 at 02:15
  • Was the activity displaying correctly before you added this new code? You might want to add some log statements or temporarily remove the new code and add it back piece by piece to see where it is getting stuck. If you know how to use the debugger that would be even better. – George Mulligan Mar 31 '16 at 02:16

0 Answers0