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

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

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

The cumulative value stored in SharedPreferences creates a problem for me (I cannot reinitialize it every time the program starts).

As a result, when I try 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 exception?

I thought about putting the getInt () call in a try-catch clause so that I could write it in the first run of the program, but then I realized: is the default value in getInt () (the 2nd parameter) meant exactly for such a case?

UPDATE: @MyBD, , :

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());

.

, java.lang.ClassCastException.

+3
5

"" , onCreate():

try {
  int dummy = prefs.getInt("cumulative-valuee", 0) + 1 ; // <== this generate exception -- why?
  Log.i("dummy ready", " ==> " + dummy);
}
catch (ClassCastException e) {
  Editor editor = prefs.edit();
  editor.putInt("cumulative-valuee", 0);  
  editor.commit();

  int dummy = prefs.getInt("cumulative-valuee", 0) + 1 ; // <== try again (should yield NO exception)
  Log.i("dummy ready", " ==> " + dummy);
}

.:)

+1

, , , :

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)

:

, , defValue. ClassCastException, , int.

, .

+4

It is discovered that this happens when you have an existing pref that is a different class than what you are requesting.

So, for example, if you saved String, but use Prefs.getInt (); this will cause an error.

+1
source

Have the prefs object been configured correctly? This does not appear in the code you specified.

0
source

I had almost the same problem. This was resolved when I assigned keys for all settings. So check yor prefrerences.xml for empty keys)

0
source

All Articles