0

I had huge problems deserializing a List<IWorkout>, since it would throw

java.lang.RuntimeException: Unable to start activity ComponentInfo{MY PACKAGE}: java.lang.RuntimeException: Unable to invoke no-args constructor for interface MY INTERFACE. Registering an InstanceCreator with Gson for this type may fix this problem.

But I solved it by deserializing it to a List<Workout> (which implements the interface IWorkout), and then adding it to a List<IWorkout>.

Code example of my solution :

public ArrayList <IWorkout> loadUserWorkoutList(){
    Gson gson = new Gson();

    Type workoutListType = new TypeToken<ArrayList<Workout>>(){}.getType();

    List <Workout> workoutList = gson.fromJson(loadWorkoutDataFromSharedPreferences(), workoutListType);

    if (workoutList == null){
        workoutList = new ArrayList<>();
    }

    ArrayList<IWorkout> iWorkoutList = new ArrayList<>(workoutList);

    return iWorkoutList;
}

But the problem now is that the Workout contains another List<IExercise> which I can't (or at least can't figure out how to) perform this "trick" on. The entire IWorkout list is serialized like this :

private String workoutListToJsonString(List<IWorkout> list) {
    Gson gson = new Gson();

    String jsonString = gson.toJson(list);

    return jsonString;
}

and it throws the same error as before when I try to deserialize it.

Does anyone have any ideas on how to solve this?

I've tried using the adapter provided by Narthi, but to no success.

Thanks in advance

zagzig
  • 69
  • 1
  • 5

0 Answers0