Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:36:06+00:00 2026-05-22T01:36:06+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:36:07+00:00Added an answer on May 22, 2026 at 1:36 am

    Here is how I “solved” the problem, by placing the following code in 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);
    }
    

    It works. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used java after a long time and found out that unlike other languages
s3c44b0x does not support flash breakpoint(unlike other processors such as lpc1758 which has on-chip
Unlike the other posts about the task delete all tables, this question is specifically
I have a programmatically generated dijit.form.Select. Unlike most other widgets, the Selects do not
PEP 8 doesn't mention the slice operator. From my understanding, unlike other operators, it
I've setup an application for my site via facebook & unlike other screenshots I
So here's the scenario- Unlike the 3000 other questions on StackOverflow about how to
Unlike Windows, GNOME and most other GUI's, OS X application programs do not all
I'm developing an iPhone web app following some of the suggestions that can be
Unlike some others, my Android SDK setup went smoothly on my new Windows 7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.