Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences.getInt("cumulative", 0) catch 22 - how to resolve?

Unlike other values, in which I can initialize in onCreate() every time the app starts:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Editor editor = prefs.edit();
    editor.putInt("re-initiative-value", 0);
    editor.commit();
  }

a cumulative value stored in SharedPreferences poses a problem for me (I can't really re-initialize it every time the program starts).

As a result, whenever I attempt prefs.getInt("cumulative-valuee", 0), I get a RuntimeException:

05-12 16:45:50.489: ERROR/AndroidRuntime(1767): FATAL EXCEPTION: main
05-12 16:45:50.489: ERROR/AndroidRuntime(1767): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent { (has extras) }} to activity {com.example.app/com.example.app.MyActivity}: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.access$2800(ActivityThread.java:125)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.os.Looper.loop(Looper.java:123)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at java.lang.reflect.Method.invoke(Method.java:521)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at dalvik.system.NativeStart.main(Native Method)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767): Caused by: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2707)

What is a good strategy to avoid this type of exception?

I was thinking of placing the call to getInt() within a try-catch clause, so that I can write it in the first ever program start, but then I realized: Isn't the default value in getInt() (the 2nd parameter) meant exactly for such case?

UPDATE: Trying to verify @MyBD's suggestion, I added the following code to dump all preferences names and values upon first run:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);

Map<String,?> allPrefs = prefs.getAll();
for (Map.Entry<String, ?> entry : allPrefs.entrySet())
  Log.i("allPrefs", entry.getKey() + "/" + entry.getValue());

The resulting log shows no type mismatch.

This must be a different problem masquerading as java.lang.ClassCastException.

like image 599
ef2011 Avatar asked Feb 01 '26 19:02

ef2011


1 Answers

From your exception it seems like you do have this name for a string preference, not that this preference doesn't exists:

05-12 16:45:50.489: ERROR/AndroidRuntime(1767): Caused by: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2707)

From documentation:

Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.

If you change its name, it should work.

like image 162
MByD Avatar answered Feb 04 '26 14:02

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!