ClassCastException in SharedPreferences getString Android Origin: On your device OSI layer 7 · Application Last updated 25 Nov 2026

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at android.app.SharedPreferencesImpl.getString

A key was saved as one type and read as another.

Exact message you may see ClassCastException SharedPreferencesImpl getStringInteger cannot be cast to java.lang.String SharedPreferencesjava.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:267)
Seen on: Android Java

Meaning

SharedPreferences returns the stored type, so reading an Int key with getString throws. Use consistent types per key or migrate with a new key name.

Common causes

  • Key written as Int, read as String
  • Library writes the same key
  • App update changed the type

⚡ Quick fix

  1. Read with the same type used to write
  2. Use a new key name
  3. Clear the old value

Detailed fix by platform

Kotlin

  1. prefs.edit().remove("count").putString("count_v2", value).apply()

How to diagnose

  1. Type — written?
  2. Key — shared?
  3. Update — changed type?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

An instance was deallocated while key value observers were still registered — NSInternalInconsistencyException: An instance 0x7f of class Foo was deallocated while key value observers were still registered with it (iOS)An instance was deall…App crashes after restore on a new phone — (behavior) crash on first launch after device transfer / Auto Backup (Android)App crashes after res…AVAudioEngine: required condition is false: format.sampleRate == hwFormat.sampleRate — Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate' (iOS)AVAudioEngine: requir…BadParcelableException: ClassNotFoundException when unmarshalling — android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.example.Model (Android)BadParcelableExceptio…ClassCastException: cannot be cast to android.widget — java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.TextView (Android)ClassCastException: c…ConcurrentModificationException while iterating — java.util.ConcurrentModificationException at java.util.ArrayList$Itr.next (Android)ConcurrentModificatio…Core Data: NSManagedObjectContext was accessed from the wrong queue — CoreData: error: Serious application error. Exception was caught during Core Data change processing (iOS)Core Data: NSManagedO…Coroutine exception crashes the app (no handler) — FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.example, PID: 1234 (Android)Coroutine exception c…ClassCast…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.