0

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?

Computerish
  • 9,590
  • 7
  • 38
  • 49
micky
  • 508
  • 5
  • 17

5 Answers5

1
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.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
1

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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
1
      //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");
Nilesh Patel
  • 137
  • 6
0

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

Community
  • 1
  • 1
Computerish
  • 9,590
  • 7
  • 38
  • 49
0

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!!!

micky
  • 508
  • 5
  • 17