【发布时间】:2017-12-28 05:54:54
【问题描述】:
我在这里尝试最佳答案中的解决方案:Android ArrayList of custom objects - Save to SharedPreferences - Serializable?
我正在尝试将自定义对象的 ArrayList 保存到 SharedPreferences。
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 是自定义对象的 ArrayList (Pin)。
错误是:
java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible
错误出现在这一行:
String json = gson.toJson(masterPinList);
怎么了?
【问题讨论】:
-
显示
Pin的代码 -
Pin 引用了应用程序和其他自定义对象。根据下面的答案,并在使用另一个自定义对象进行测试后,我发现在不引用应用程序中的上下文或其他自定义对象的情况下制作 Pin 对象会更容易。
标签: java android arraylist gson sharedpreferences