title: SharedPreferences.getInt("cumulative", 0) catch 22 - how to resolve?
body: <p>Unlike other values, in which I can initialize in <code>onCreate()</code> every time the app starts:</p>
  
  <pre><code>  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Editor editor = prefs.edit();
      editor.putInt("re-initiative-value", 0);
      editor.commit();
    }
  </code></pre>
  
  <p>a cumulative value stored in SharedPreferences poses a problem for me (I can't really re-initialize it every time the program starts).</p>
  
  <p>As a result, whenever I attempt <code>prefs.getInt("cumulative-valuee", 0)</code>,  I get a RuntimeException:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>What is a good strategy to avoid this type of exception?</p>
  
  <p>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?</p>
  
  <p>UPDATE: Trying to verify @MyBD's suggestion, I added the following code to dump all preferences names and values upon first run:</p>
  
  <pre><code>PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
  prefs = PreferenceManager.getDefaultSharedPreferences(this);
  
  Map&lt;String,?&gt; allPrefs = prefs.getAll();
  for (Map.Entry&lt;String, ?&gt; entry : allPrefs.entrySet())
    Log.i("allPrefs", entry.getKey() + "/" + entry.getValue());
  </code></pre>
  
  <p>The resulting log shows no type mismatch.</p>
  
  <p>This must be a different problem masquerading as <code>java.lang.ClassCastException</code>.</p>