0

I am trying the solution in the top answer here: Android ArrayList of custom objects - Save to SharedPreferences - Serializable?

I am trying to save to SharedPreferences an ArrayList of custom objects.

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(baseApplication);
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(masterPinList);
prefsEditor.putString("masterPinList", json);
prefsEditor.apply();

masterPinList is an ArrayList of custom objects (Pin).

The error is:

java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible

The error occurs at this line:

String json = gson.toJson(masterPinList);

What is wrong?

user1755043
  • 281
  • 1
  • 13
  • Show the code for `Pin` – weston Jul 22 '17 at 03:23
  • Pin has references to the application and other custom objects. Based on answer below, and after testing with another custom object, I found that it would be easier to make a Pin object without references to contexts or other custom objects in the app. – user1755043 Jul 22 '17 at 03:28

2 Answers2

0

you can try to add an empty constructor to the class as this will used to set values.

public className() {

}
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
0

Is your object serializable ? It could be the problem. Here is a example class

public class PlanDateObject {

@SerializedName("DateList")
private List<String> dateList;
@SerializedName("PlanList")
private List<Plan> planList;

public List<String> getDateList() {
    return dateList;
}

public void setDateList(List<String> dateList) {
    this.dateList = dateList;
}

public List<Plan> getPlanList() {
    return planList;
}

public void setPlanList(List<Plan> planList) {
    this.planList = planList;
}

}

FnR
  • 75
  • 1
  • 5
  • Would every other custom object referenced by this object also need to be serializable, because just making Pin serializable did not not work? – user1755043 Jul 22 '17 at 03:08
  • Yes. I tested your code with this class and it worked – FnR Jul 22 '17 at 03:10