3

I'm confused with an exception that is thrown for something that shouldn't. The error is the following:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String

But, my code doesn't try to cast a Boolean to String Ô_o Is it possible that the compiler is wrong in indicating where the exception is coming from? I'm using an Huawei

This part triggers the exception:

if(SharedPreferencesManager.getUserMRZ(ScanEUID.this).equals("0")
&& SharedPreferencesManager.getUserDocumentIdFront(ScanEUID.this) != null 
&& SharedPreferencesManager.getUserDocumentIdBack(ScanEUID.this) != null)
{

The associated methods are the following

public static String getUserMRZ(Context context){
    return getSharedPreferences(context).getString(Constants.USER_HAS_MRZ, "0");
}
public static String getUserDocumentIdFront(Context context) {
    return getSharedPreferences(context).getString(Constants.USER_DOCUMENT_ID_FRONT, null);
}
public static String getUserDocumentIdBack(Context context) {
    return getSharedPreferences(context).getString(Constants.USER_DOCUMENT_ID_BACK, null);
}

The setMRZ method

public static void setUserMRZ(Context context, String has_mrz){
        final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(Constants.USER_HAS_MRZ, has_mrz);
        editor.apply();
}
Isabelle
  • 1,457
  • 5
  • 19
  • 44
  • 1
    If there's ever been a string with that key (USER_HAS_MRZ), even if by accident, it will stay there until you clear the app's data or reinstall. Try reinstalling it to see if it still occurs. – Denny May 10 '17 at 14:39
  • 2
    Check where you are first creating the USER_HAS_MRZ preference. You are probably informing a boolean value. – Jonas452 May 10 '17 at 14:41
  • Uninstalled it and unfortunately same error. So weird. – Isabelle May 10 '17 at 14:44
  • Thank you @Jonas452 - I just updated my question to add the method that set it. So I have 3 usages and the first is : "SharedPreferencesManager.setUserMRZ(identityCheck.this, "0");" – Isabelle May 10 '17 at 14:47

5 Answers5

5

Check your setters, I just had the same issue and I discovered that I'd mistakenly done a bad copy/pasta and used the Key for that value in a setter for a Boolean value...

AZR34L
  • 73
  • 2
  • 6
  • You saved my mental and about half of an hour of my time... Really thanks a lot. – Han Park Jan 05 '20 at 12:14
  • 2
    If the app you've installed is using a boolean preference and you now want it to be a String, you must reinstall your app. – Rafael Oct 12 '21 at 12:24
1

In My case I was wrongly used the same shared preference key for different Activity, For putString and putBoolean. Though the problem arise. I used an activity as :

editor.putString("fezilalilquran", "on");

And Another Activity I used :

editor.putBoolean("fezilalilquran", true);

So, you see that, for the same key word for boolean and string in Shared Preferences, When I want to get the boolean in MainActivity, so the cast exception arise for this line :

boolean isTafseerOn = settings.getBoolean("fezilalilquran", true);

So, replacing the line in settings Activity, from :

editor.putString("fezilalilquran", "on");

to :

editor.putBoolean("fezilalilquran", true);

solved my issue:

Happy coding...... .

Noor Hossain
  • 1,620
  • 1
  • 18
  • 25
0

Look at the documentation

https://developer.android.com/reference/android/content/SharedPreferences.html#getString(java.lang.String, java.lang.String)

If your value of Constants.USER_HAS_MRZ is Boolean, you should use getBoolean method

https://developer.android.com/reference/android/content/SharedPreferences.html#getBoolean(java.lang.String, boolean)

Deni Erdyneev
  • 1,779
  • 18
  • 26
0

I had this error and found that I used the same key twice (copy/paste error).

private const val PREF_IS_IMPERIAL ="imperial"
private const val PREF_LAST_CITY_SEARCH ="imperial"  //oops
seekingStillness
  • 4,833
  • 5
  • 38
  • 68
-3

In my case, I just surround the get methods with Try Catch and it works!

  • It is not answering the question, instead it just tells to catch the exception. – jrh May 31 '20 at 09:55