0

I'm trying to save an int I get from an EditText, and as people said it will be best with SharedPreference, I listened, but everytime i'm trying to save/load, the program crashes! any ideas?

public static final String MY_PREFS_NAME = "MyPrefsFile";
String getsturdvalue;
int sturd;
EditText sturdadapter;
int sturdadaptercount;

public void onSave(View view) {
    SharedPreferences.Editor editor = (SharedPreferences.Editor) getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    editor.putInt(getstrengthvalue, strnth);
    editor.apply();
}

public void onLoad(View view) {
    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    String restoredText= prefs.getString("text", null);
    if (restoredText != null) {
        int insertvalue = prefs.getInt("insertvalue", strnth);
        int adapter = prefs.getInt("adapter", strengthadaptercount);
    }
    strengthadapter.setText(prefs.getInt("" ,strengthadaptercount));
}
Termininja
  • 5,689
  • 12
  • 40
  • 45
A Man
  • 11
  • 3

1 Answers1

0

It seems a ClassCastException. Try this:

public void onSave(View view) {
    SharedPreferences.Editor editor = (SharedPreferences.Editor)
        getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    // ...
}
nandsito
  • 3,634
  • 2
  • 16
  • 25