1

I am trying to use SharedPreferences to store some values and retrieve them when the app opens, but I have issues with retrieving some int values.

I have tried converting the value to a string and then using Integer.ParseInt to retrieve the value, but I still get an error.

This is where my value should be stored:

void putP1Index(int index) {
    SharedPreferences.Editor mEditor = mPrefs.edit();
    mEditor.putInt(keyP1Index, index);
    mEditor.apply();
}

void putP2Index(int index) {
    SharedPreferences.Editor mEditor = mPrefs.edit();
    mEditor.putInt(keyP2Index, index);
    mEditor.apply();
}

They are saved from a different class:

private MainGame mainGame;
private SavedValues savedValues;


ScoreSetter(MainGame mainGame) {
    this.mainGame = mainGame;
    savedValues = new SavedValues(mainGame);
}

void setScore(who) {
switch (who) {
        case 1:
            ++indexP1;
            savedValues.putP1Index(indexP1);
//Some code...
        case 2:
            ++indexP2;
            savedValues.putP2Index(indexP2);
//Some code...
}
}

And I try to retrieve it like this:

int getP1Index() {
    return mPrefs.getInt(keyP1Index, 0);
}

int getP2Index() {
    return mPrefs.getInt(keyP2Index, 0);
}

by calling them from a different class:

void getSavedValues() {
    indexP1 = savedValues.getP1Index();
    indexP2 = savedValues.getP2Index();
}

On the getSavedValues method I get the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

What I don't understand is the fact that I save the value as int and try to retrieve them the same way.

Also, I have a similar method but with booleans and it works just fine.

6
  • geeksforgeeks.org/string-to-integer-in-java-parseint Commented Feb 15, 2019 at 22:09
  • Are you sure you're using the same key to save your int as you are to retrieve it? What are keyP1Index and keyP2Index at the time you save and retrieve your values? Commented Feb 15, 2019 at 22:10
  • Post the full code Commented Feb 15, 2019 at 22:26
  • 1
    @YasiruNayanajith Updated the code. Added what is relevant to this case as I have a lot of code that is irrelevant for this. As you can see, I am working only with integers and this is what baffles me. Commented Feb 16, 2019 at 10:27
  • Could it be that the value under the key keyP1Index or keyP2Index is a string written in a previous version of the app? Commented Feb 16, 2019 at 10:42

1 Answer 1

2

I managed to fix the issue. There was nothing wrong with the code, but with a value that was written in a previous version of the app.

After cleaning the app data and and Invalidate and clear cache in Android Studio the issue disappeared.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You saved my day! I had a similar problem in Flutter using the plugins 'permission_handler' and 'geolocator'. And the solution to this problem was also this Android Studio "feature".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.