Java Xamarin MultiSelectListPreference无效CastException

Java Xamarin MultiSelectListPreference无效CastException,java,c#,android,.net,xamarin,Java,C#,Android,.net,Xamarin,目前,我正试图在Android的共享首选项存储中保存一个字符串列表。我使用共享首选项,因此可以使用Android本身的MultiSelectListPreference。保存价值不是问题,但可能是原因。当我尝试读取值时,问题本身就开始了。此时,Android为Xamarin代码提供了一个ArrayList,Xamarin应该将其转换为C列表。然后我得到了以下无效的例外: [MonoDroid] UNHANDLED EXCEPTION: [MonoDroid] System.InvalidCast

目前,我正试图在Android的共享首选项存储中保存一个字符串列表。我使用共享首选项,因此可以使用Android本身的MultiSelectListPreference。保存价值不是问题,但可能是原因。当我尝试读取值时,问题本身就开始了。此时,Android为Xamarin代码提供了一个ArrayList,Xamarin应该将其转换为C列表。然后我得到了以下无效的例外:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Cannot cast from source type to destination type.
[MonoDroid] at (wrapper castclass) object.__castclass_with_cache (object,intptr,intptr) <0x00068>
[MonoDroid] at Android.Runtime.JavaSet`1<string>.FromJniHandle (intptr,Android.Runtime.JniHandleOwnership) <0x0008f>
[MonoDroid] at Android.Content.ISharedPreferencesInvoker.GetStringSet (string,System.Collections.Generic.ICollection`1<string>) <0x0015b>
[MonoDroid] at canvastix.MultiSelectListPreference.OnDialogClosed (bool) <0x0009f>
[MonoDroid] at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (intptr,intptr,bool) <0x0003f>
[MonoDroid] at (wrapper dynamic-method) object.4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool) <0x00043>
[mono] 
[mono] Unhandled Exception:
[mono] System.InvalidCastException: Cannot cast from source type to destination type.
[mono]   at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
[mono]   at Android.Runtime.JavaSet`1[System.String].FromJniHandle (IntPtr handle, JniHandleOwnership transfer) [0x00000] in <filename unknown>:0 
[mono]   at Android.Content.ISharedPreferencesInvoker.GetStringSet (System.String key, ICollection`1 defValues) [0x00000] in <filename unknown>:0 
[mono]   at canvastix.MultiSelectListPreference.OnDialogClosed (Boolean positiveResult) [0x00000] in <filename unknown>:0 
[mono]   at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (IntPtr jnienv, IntPtr native__this, Boolean positiveResult) [0x00000] in <filename unknown>:0 
[mono]   at (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool)
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
[mono-rt]   at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
[mono-rt]   at Android.Runtime.JavaSet`1[System.String].FromJniHandle (IntPtr handle, JniHandleOwnership transfer) [0x00000] in <filename unknown>:0 
[mono-rt]   at Android.Content.ISharedPreferencesInvoker.GetStringSet (System.String key, ICollection`1 defValues) [0x00000] in <filename unknown>:0 
[mono-rt]   at canvastix.MultiSelectListPreference.OnDialogClosed (Boolean positiveResult) [0x00000] in <filename unknown>:0 
[mono-rt]   at Android.Preferences.DialogPreference.n_OnDialogClosed_Z (IntPtr jnienv, IntPtr native__this, Boolean positiveResult) [0x00000] in <filename unknown>:0 
[mono-rt]   at (wrapper dynamic-method) object:4bc48339-d8f3-443a-a584-0088f55fe375 (intptr,intptr,bool)
我使用以下代码读取活动中的值。没什么特别的:

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();
};
我使用以下代码在片段中显示MultiSelectListPreference:

<?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>
这是活动的布局:

<?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 and Show" />
</LinearLayout>
最后但并非最不重要的是,以下是列表中的值:

<?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>
请随便试用。在清单中,我将minSdkVersion设置为15,targetSdkVersion设置为19


一周后我的老师告诉我这一定是Xamarin的一只虫子。因此,如果你找到一个解决方法,那也会很好。

你可能遇到了这个Xamarin bug: 看起来它在一月份被标记为固定的

但请参见Xamarin论坛上的最新讨论:


如果您没有使用最新的Xamarin版本,您可能希望尝试使用它。

您无法将字符串集合保存到共享首选项中,只能保存字符串、int、boolean等基本类型。请尝试将集合转换为某些字符串,如StringBuilder=new StringBuilder;forString s:arr{builder.appends;}返回builder.toString;Xamarin应该能够将c字符串列表保存到java字符串列表中。问题是MultiSelectListPreference处理save和retrieve方法,这意味着我必须覆盖几乎每一个方法才能使其工作。更不用说Xamarin将来的更新可能会破坏我的扩展类。我宁愿使用standaard MultiSelectListPreference。为什么不将其转换为逗号分隔的值并保存呢?几周前我也看到了它。恐怕它可能会回来。但是看到它没有那么多的反应,我怀疑他们会很快解决这个问题。与此同时,我想我正在寻找影响尽可能小的解决办法。我想知道你是否有一些旧的损坏数据,或者你是否在项目的其他地方写出了一些无效数据。在我的另一个项目中,我刚刚插入了一些MultiSelectListPreference首选项,它们似乎工作得很好。Prefs.GetStringSet和Prefs.PutStringSet似乎也都工作正常。