-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer SeekBarPreference.onSetInitialValue #81
Comments
Hi, what have you tried so far? Looks like somehow a string got into a preference that's supposed to be integer.
I recommend Stetho for investigating what's actually stored in there in case this happens on your own device. |
I've checked these but I couldn't find anything. This is how I use it:
Can it be that |
It's not XML related, it crashes because it's trying to load the value from hard drive and it finds a String instead of the expected integer. Did you by any chance use the same key "volume" for anything else? Or did you edit shared preferences manually? This is the only thing that comes to my mind... Unless... "volume" is a very generic name, some library you're using may be writing in your default shared preference file.
In that case the default value is |
Is ClasscastException fatal error? |
It is. |
@dirkam I never asked, what device did this happen on? Can you reproduce it? |
@dirkam Could you release a version of your app with some added logging? I'd like to see what value is stored in that preference. Perhaps then I'll be able to guess where it came from. When your settings activity starts, before it loads the preference fragment, check if the preference file contains the value and if it does, try to get it. You'll need to try-catch all the types because we don't know what's the type of value.
You can get the default preferences with
|
Thanks, will do that and keep monitoring it. |
Finally, I managed to catch this live (based on a user report, so I'm still unable to reproduce it locally). This is extremely weird. I have 5
This is really weird. I've checked all the relevant code 3 times and I don't put Not sure if this helps. I think I can add some workaround locally but would be great to get to the bottom of this. |
After adding the workaround (basically, I check for these errors and load a default value if there is an issue), it doesn't crash on the user's device anymore. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at java.lang.reflect.Method.invoke(Native Method)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at android.os.Looper.loop(Looper.java:154)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)
at android.app.Activity.performStart(Activity.java:6973)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
at android.support.v7.app.e.onStart(AppCompatActivity.java:177)
at android.support.v4.app.g.onStart(FragmentActivity.java:16192)
at android.support.v4.app.l.q(FragmentManager.java:3181)
at android.support.v4.app.l.c(FragmentManager.java:3231)
at android.support.v4.app.l.k(FragmentManager.java:2239)
at android.support.v4.app.l.a(FragmentManager.java:2332)
at android.support.v4.app.l.a(FragmentManager.java:2374)
at android.support.v4.app.q.a(FragmentTransition.java:115)
at android.support.v4.app.q.a(FragmentTransition.java:1070)
at android.support.v4.app.q.a(FragmentTransition.java:1187)
at android.support.v4.app.l.a(FragmentManager.java:1377)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2246)
at android.support.v7.preference.f.onCreate(PreferenceFragmentCompat.java:224)
at android.support.v7.preference.m.a(XpPreferenceFragment.java:54)
at com.myapp.q.b(SettingsFragment.java:)
at android.support.v7.preference.q.a(XpPreferenceManager.java:67)
at android.support.v7.preference.h.a(PreferenceInflater.java:112)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:103)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:293)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:293)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:293)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:293)
at android.support.v7.preference.p.a(XpPreferenceInflater.java:4126)
at android.support.v7.preference.PreferenceGroup.b(PreferenceGroup.java:194)
at android.support.v7.preference.Preference.a(Preference.java:1256)
at android.support.v7.preference.Preference.a(Preference.java:6516)
at net.xpece.android.support.preference.MultiSelectListPreference.a(MultiSelectListPreference.java:195)
at android.support.v7.preference.l.b(XpPreferenceCompat.java:90)
at net.xpece.android.support.preference.m.a(SharedPreferencesCompat.java:39)
at net.xpece.android.support.preference.m.b(SharedPreferencesCompat.java:52)
at org.json.JSONArray.(JSONArray.java:108)
at org.json.JSONArray.(JSONArray.java:92)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
org.json.JSONException: End of input at character 0 of |
Can you say what devices do the crashes come from (model, amdroid version)? |
This occurrence was on a Samsung A5 running Android 7. |
The stacktrace may be useful. What version of the lib were you using? I'll need to match the line numbers. Thanks |
1.3.2 |
First some background on the last error: I used to store string sets as Strings with JSON arrays on Gingerbread. When reading the preference I tried to read it as Set and if it failed, fall back to parsing String as JSON array and reconstruct the Set from that. Now, wrong data is stored instead of Set so it mistakenly tries to parse it as JSON array from String. The preference contained an empty string it looks. It shouldn't contain a string at all on Android 7. A String was also stored in the SeekBarPreference. I don't see where this comes from. |
Got this with the new error reporting stuff.
Non-fatal Exception: org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONArray.(JSONArray.java:92)
at org.json.JSONArray.(JSONArray.java:108)
at net.xpece.android.support.preference.SharedPreferencesCompat.getStringSetFromJson(SharedPreferencesCompat.java:66)
at net.xpece.android.support.preference.SharedPreferencesCompat.getStringSet(SharedPreferencesCompat.java:56)
at android.support.v7.preference.XpPreferenceCompat.getPersistedStringSet(XpPreferenceCompat.java:90)
at android.support.v7.preference.XpPreferenceCompat.persistStringSet(XpPreferenceCompat.java:59)
at net.xpece.android.support.preference.MultiSelectListPreference.setValues(MultiSelectListPreference.java:138)
at net.xpece.android.support.preference.MultiSelectListPreference.onSetInitialValue(MultiSelectListPreference.java:197)
at android.support.v7.preference.Preference.dispatchSetInitialValue(Preference.java:6519)
at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1257)
at android.support.v7.preference.PreferenceGroup.addPreference(PreferenceGroup.java:234)
at android.support.v7.preference.XpPreferenceInflater.android.support.v7.preference.PreferenceGroup.addItemFromInflater(XpPreferenceInflater.java:4166)
at android.support.v7.preference.XpPreferenceInflater.rInflate(XpPreferenceInflater.java:293)
at android.support.v7.preference.XpPreferenceInflater.rInflate(XpPreferenceInflater.java:293)
at android.support.v7.preference.XpPreferenceInflater.rInflate(XpPreferenceInflater.java:293)
at android.support.v7.preference.XpPreferenceInflater.rInflate(XpPreferenceInflater.java:293)
at android.support.v7.preference.XpPreferenceInflater.inflate(XpPreferenceInflater.java:103)
at android.support.v7.preference.PreferenceInflater.inflate$7af30e91(PreferenceInflater.java:112)
at android.support.v7.preference.XpPreferenceManager.inflateFromResource$6154c3e6(XpPreferenceManager.java:69)
at .SettingsFragment.android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(SettingsFragment.java)
at android.support.v7.preference.XpPreferenceFragment.onCreatePreferences(XpPreferenceFragment.java:56)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:224)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentActivity.android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentActivity.java:11195)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
at android.app.Activity.performStart(Activity.java:6959)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) |
There are two potential issues here: 1) Reading the preferenceDoes your MultiSelectListPreference have a default value? Is it an empty string? If so, it should instead
String, even an empty one, cannot be parsed as a Set by the preference framework. I will make this throw as it should in the next version of the library. 2) Writing the preferenceThis is the non-fatal you reported in the previous comment. I'll fix the library so it doesn't throw ClassCastException when writing preferences. Framework doesn't throw, my MultiSelectListPreference erroneously did. Thanks for catching it. |
Please try and test the current snapshot and let me know if you encounter any errors.
|
I'll try the snapshot version, though I can't reproduce these so not sure if I'll notice anything. I don't set a default value in XML. I set the value runtime and here it can happen that I pass a set like this without any value: |
just uninstall your app and then install it. look like you saved string on that key before. |
Can you please look into this crash? From time to time I see this crash reported, but it's a rare one.
The text was updated successfully, but these errors were encountered: