0
votes

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?

2
Show the code for Pinweston
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

2 Answers

0
votes

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

public className() {

}
0
votes

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

}