java - Xamarin MultiSelectListPreference InvalidCastException -


currently i'm trying save list of string in shared preferences storage of android. use shared preferences , therefore can use multiselectlistpreference of android itself. saving value not issue, maybe it's cause. issue starts when try read value. @ moment android gives arraylist xamarin code , xamarin should convert c# list. , following invalidcastexception:

[monodroid] unhandled exception: [monodroid] system.invalidcastexception: cannot cast source type destination type. [monodroid] @ (wrapper castclass) object.__castclass_with_cache (object,intptr,intptr) <0x00068> [monodroid] @ android.runtime.javaset`1<string>.fromjnihandle (intptr,android.runtime.jnihandleownership) <0x0008f> [monodroid] @ android.content.isharedpreferencesinvoker.getstringset (string,system.collections.generic.icollection`1<string>) <0x0015b> [monodroid] @ canvastix.multiselectlistpreference.ondialogclosed (bool) <0x0009f> [monodroid] @ android.preferences.dialogpreference.n_ondialogclosed_z (intptr,intptr,bool) <0x0003f> [monodroid] @ (wrapper dynamic-method) object.4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool) <0x00043> [mono]  [mono] unhandled exception: [mono] system.invalidcastexception: cannot cast source type destination type. [mono]   @ (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr) [mono]   @ android.runtime.javaset`1[system.string].fromjnihandle (intptr handle, jnihandleownership transfer) [0x00000] in <filename unknown>:0  [mono]   @ android.content.isharedpreferencesinvoker.getstringset (system.string key, icollection`1 defvalues) [0x00000] in <filename unknown>:0  [mono]   @ canvastix.multiselectlistpreference.ondialogclosed (boolean positiveresult) [0x00000] in <filename unknown>:0  [mono]   @ android.preferences.dialogpreference.n_ondialogclosed_z (intptr jnienv, intptr native__this, boolean positiveresult) [0x00000] in <filename unknown>:0  [mono]   @ (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool) [mono-rt] [error] fatal unhandled exception: system.invalidcastexception: cannot cast source type destination type. [mono-rt]   @ (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr) [mono-rt]   @ android.runtime.javaset`1[system.string].fromjnihandle (intptr handle, jnihandleownership transfer) [0x00000] in <filename unknown>:0  [mono-rt]   @ android.content.isharedpreferencesinvoker.getstringset (system.string key, icollection`1 defvalues) [0x00000] in <filename unknown>:0  [mono-rt]   @ canvastix.multiselectlistpreference.ondialogclosed (boolean positiveresult) [0x00000] in <filename unknown>:0  [mono-rt]   @ android.preferences.dialogpreference.n_ondialogclosed_z (intptr jnienv, intptr native__this, boolean positiveresult) [0x00000] in <filename unknown>:0  [mono-rt]   @ (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool) 

i use following piece of code read value in activity. nothing special:

button button = findviewbyid<button> (resource.id.button); button.click += delegate {     //read value     isharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (this);     icollection<string> list = prefs.getstringset("list", new list<string>());     toast.maketext(this, "total items: " + list.count, toastlength.short).show(); }; 

i use following code show multiselectlistpreference in fragment:

<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">     <multiselectlistpreference         android:key="list"         android:entries="@array/entries"         android:entryvalues="@array/values" /> </preferencescreen> 

this layout of activity:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <fragment         class="xamarinbugreadstringsetpreference.mainfragment"         android:id="@+id/fragment"         android:layout_width="match_parent"         android:layout_height="match_parent" />     <button         android:id="@+id/button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="read , show" /> </linearlayout> 

and last not least, these values of list:

<?xml version="1.0" encoding="utf-8" ?> <resources>     <string-array name="entries">         <item>1</item>         <item>2</item>     </string-array>     <string-array name="values">         <item>item 1</item>         <item>item 2</item>     </string-array> </resources> 

feel free try out. in manifest have set minsdkversion 15 , targetsdkversion 19.

my taught after week must bug in xamarin. if find work-around great well.

you may have run xamarin bug: https://bugzilla.xamarin.com/show_bug.cgi?id=13141 looks marked fixed in january.

(but see latest in discussion in xamarin forum): http://forums.xamarin.com/discussion/6531/preferencemanager-getstringset-keeps-crashing

if not using latest xamarin builds may want try that.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -