PreferenceManager, GetStringSet keeps crashing RRS feed

  • Question

  • User8397 posted

    I'm trying to save and retrieve a List values. I've tried everything I can think of but when trying to fetch these values I get "java.util.ArrayList cannot be cast to java.util.Set".

    MonoDroid: 4.8.0 OSX: 10.8.4

    Saving as follows: (val is a List<string>)

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);

    Trying to fetch:

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);
    var result = prefs.GetStringSet(key, new List<string>());

    Thanks -c

    Tuesday, July 30, 2013 8:11 PM

All replies

  • User12620 posted

    I have the same problem. As a consequence, I can't use MultiSelectListPreference.Values property.

    java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set at android.app.SharedPreferencesImpl.getStringSet(SharedPreferencesImpl.java:213)

    Win 7 x64, MonoDroid version:4.8.13 Visual Studio Ultimate 2012 Update 2

    Well, I see that code:

    Set v = (Set) mMap.get(key);

    at Java source code of android.app.SharedPreferencesImpl.getStringSet(). Field mMap is a HashSet that holds parsed data from preferences file. If "v" object holds preference of unexpected type, then casting crushes.

    A method "android.content.SharedPreferences.editor.PutStringSet" has a corresponding syntax: (String key, Set values).

    Object of Set is received, saved, and then it's loaded and casted back to Set.

    But in C# source of method "ISharedPreferenceEditor.PutStringSet" (and deeper) we see:

    jvalueArray[index2] = new JValue((IJavaObject) JavaObjectExtensions.ToInteroperableCollection(values));


    return new JavaCollection((IEnumerable) instance)


    internal JavaCollection(IEnumerable items) : base(JNIEnv.CreateInstance("java/util/ArrayList", "()V", new JValue[0]), JniHandleOwnership.TransferLocalRef)

    I'm not good in internal mechanism of Xamarin.Android, but it seems to me that the object of wrong type is given to putStringSet.

    Friday, August 2, 2013 1:19 PM
  • User1658 posted

    There is a bug filed in BugZilla: https://bugzilla.xamarin.com/show_bug.cgi?id=13141 I have referenced this thread there.

    Wednesday, August 7, 2013 5:06 PM
  • User24226 posted

    I'm seeing this error again in 4.14, anyone else?

    at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/41933531/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:194
    at Android.Runtime.JNIEnv.FindClass (System.String classname) [0x00037] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/41933531/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:388
    --- End of managed exception stack trace ---
    java.lang.ClassNotFoundException: Didn't find class "android.runtime.JavaSet_1" on path: DexPathList[[zip file "/data/app/APPNAME.apk"],nativeLibraryDirectories=[/data/app-lib/APPNAME, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at mono.java.lang.RunnableImplementor.n_run(Native Method)
        at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
        at dalvik.system.NativeStart.main(Native Method)
    Friday, August 8, 2014 2:32 PM
  • User44771 posted

    I am having Xamarin.Android 5.1. This hasn't been resolved it seems. InvalidCastException

    Tuesday, September 15, 2015 6:08 AM