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.