我想在这里顶端应答解决方案:Android ArrayList of custom objects - Save to SharedPreferences - Serializable?无法保存使用GSON SharedPreferences定制的ArrayList的对象
我想保存到SharedPreferences自定义对象的ArrayList。
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
)。
的错误是:
String json = gson.toJson(masterPinList);
什么是错的:在此行中出现
java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible
的错误?
显示为'Pin' – weston
PIN码的应用和其他自定义对象的引用代码。基于下面的答案,并且在用另一个自定义对象进行测试后,我发现在应用中没有对上下文或其他自定义对象的引用的情况下制作Pin对象会更容易。 – user1755043