Senthamil Thendral S
Senthamil Thendral S

Reputation: 508

Unable to access sharedpreference from one class to another

Hi I am developing android openGL live wallpaper. I am unable to access shared preference from wallpaperService to renderer class.I am getting the error like:

06-26 01:42:38.285: E/AndroidRuntime(4778): FATAL EXCEPTION: main
06-26 01:42:38.285: E/AndroidRuntime(4778): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
06-26 01:42:38.285: E/AndroidRuntime(4778):     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
06-26 01:42:38.285: E/AndroidRuntime(4778):     at com.srashtaa.planets3dgalaxylivewallpaper.NeheLesson08Renderer.LoadPreferences(NeheLesson08Renderer.java:155)
06-26 01:42:38.285: E/AndroidRuntime(4778):     at com.srashtaa.planets3dgalaxylivewallpaper.NeheLesson08Renderer.setContext(NeheLesson08Renderer.java:146)
06-26 01:42:38.285: E/AndroidRuntime(4778):     at com.srashtaa.planets3dgalaxylivewallpaper.NeheLesson08WallpaperService$MyEngine.<init>(NeheLesson08WallpaperService.java:42)
06-26 01:42:38.285: E/AndroidRuntime(4778):     at com.srashtaa.planets3dgalaxylivewallpaper.NeheLesson08WallpaperService.onCreateEngine(NeheLesson08WallpaperService.java:26)

This is my code:

         sharedPreferences = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
         String rotspeed = null;
         String storedPreference = sharedPreferences.getString("rotspeed", "key");
         SharedPreferences.Editor editor = sharedPreferences.edit();
         editor.putString("rotspeed", storedPreference); // value to store
         editor.commit();
         String check=sharedPreferences.getString("rotspeed", "");
         System.out.println("stored procedure values::"+check);

Here I am trying to get preference value,

               prefs= PreferenceManager.getDefaultSharedPreferences(value);
               storedPreference= prefs.getString("rotspeed","0");

Can anyone tell me why I get this error?

Upvotes: 0

Views: 608

Answers (5)

Senthamil Thendral S
Senthamil Thendral S

Reputation: 508

Thanks for all who answered my question.All answers are appreciated.It was my mistake when I calling the preferences from another activity,the activity doesn't hold its context.Thatsy I unable to access preferences.

           public void setContext(Context value) {
mContext = value;

    prefs = value.getSharedPreferences(PREFERENCES,NeheLesson08SettingsActivity.MODE_PRIVATE);


}

I failed to place "value" infront of getSharedPreferences.

Thanks!!!

Upvotes: 0

Nilesh Patel
Nilesh Patel

Reputation: 137

      //Try This 

        SharedPreferences sharedPreferences =  PreferenceManager.getDefaultSharedPreferences(this);

        Editor editor = sharedPreferences.edit();

        editor.putString("rotspeed", value);

        editor.commit();

   }

   SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
   String name = sharedPreferences.getString("rotspeed", "1000");

Upvotes: 1

Computerish
Computerish

Reputation: 9591

The two parameters to editor.putString() are the key and the string you want to store, not a reference to the SharedPreferences object. For example

editor.putString("rotspeed", "1000")

See here for the documentation of SharedPreferences.Editor:

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

And here for a couple good introductions to using SharedPreferences in general:

http://developer.android.com/training/basics/data-storage/shared-preferences.html How to use SharedPreferences in Android to store, fetch and edit values

Upvotes: 0

Opiatefuchs
Opiatefuchs

Reputation: 9870

You are trying to get a String preference from where You had put some integer preference. You need to show us the code, so we can help You. I think You have done something like this:

 mEditor.putInt("KEY",1);

and You are trying to get this value with:

 mPrefs.getString("KEY");

Please show us Your code.

Upvotes: 1

Shivam Verma
Shivam Verma

Reputation: 8023

06-26 01:42:38.285: E/AndroidRuntime(4778): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

This line states that you're trying to cast an Integer to String. Hence the crash. Make sure that if you've saved a value as String in your SharedPrefs, you're retrieving it as a String.

Upvotes: 1

Related Questions