hi I did these processes to save and load SharedPreferences
a few locations, I have 3 buttons and when I press the current location is saved, now the problem is this if I save the location in one of the buttons I close the application and I open it again and I save another button when closing and reopening the application pulls error. but if in the first execution I keep the 3 locations the application works perfect
this is my saved and loaded code
public void guardarpreferencias() {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
Gson gson2 = new Gson();
Gson gson3 = new Gson();
String json = gson.toJson(loccasa);
editor.putString("localizacionC", json);
String json2 = gson2.toJson(loctrabajo);
editor.putString("localizacionT", json2);
String json3 = gson3.toJson(locfacultad);
editor.putString("localizacionF", json3);
editor.apply();
}
public void cargarpreferencias(){
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
Gson gson = new Gson();
Gson gson2 = new Gson();
Gson gson3 = new Gson();
String json = preferences.getString("localizacionC",null );
Type type = new TypeToken<Location>() {}.getType();
loccasa = gson.fromJson(json, type);
String json2 = preferences.getString("localizacionT", null);
Type type2 = new TypeToken<Location>() {}.getType();
loctrabajo = gson2.fromJson(json2, type2);
String json3 = preferences.getString("localizacionF", null);
Type type3 = new TypeToken<Location>() {}.getType();
locfacultad = gson3.fromJson(json3, type3)
}
what to create several gsons and several type was to test mode
this is what I get in logcat
Caused by: java.lang.RuntimeException: Failed to invoke protected java.lang.ClassLoader () with no args
Caused by: java.lang.InstantiationException: Can not instantiate abstract class java.lang.ClassLoader