SharedPreferences just save me and load a data

1

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

    
asked by Houth 04.07.2018 в 04:14
source

1 answer

0

You are not defining a file name of preferences therefore you are supposed to be using getDefaultSharedPreferences () , so this is the correct way to save the preferences (context is required if you are not in a Activity ):

public void guardarpreferencias(Context context) {
    //SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    //SharedPreferences.Editor editor = preferences.edit();
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
      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();
}

to get the data:

  public void cargarpreferencias(Context context){
        //SharedPreferences preferences = getPreferences(MODE_PRIVATE);
         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

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

You can also use getSharedPreferences , I suggest you check:

Save SharedPreferences by assigning a key using getDefaultSharedPreferences ()

    
answered by 06.07.2018 в 22:58