Naruto
Naruto

Reputation: 9634

reading preferences as int throws exception

I'm trying to read the value stored in preferences as Int, its throwing me classcast exception. Here is the code

SharedPreferences prefs = ct.getSharedPreferences("volumepreference", 0);
        SharedPreferences.Editor editor = prefs.edit();

        int Past_phone_audio_mode = -1;

        try
        {
            Past_phone_audio_mode = prefs.getInt("volumestate", -1);
        }
        catch(Exception ee)
        {

        }

May i know whats wrong in my code

Upvotes: 0

Views: 43

Answers (1)

Phantômaxx
Phantômaxx

Reputation: 38098

To save a preference int value:

    final SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    // Get preference in editor mode
    final SharedPreferences.Editor editor = prefs.edit();

    // Set the Integer value
    editor.putInt("volumestate", 1);

    // Finally, save changes
    editor.apply();

Upvotes: 2

Related Questions