首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin MultiSelectListPreference InvalidCastException

Xamarin MultiSelectListPreference InvalidCastException
EN

Stack Overflow用户
提问于 2014-11-13 17:51:01
回答 1查看 418关注 0票数 0

目前,我正在尝试将字符串列表保存到Android的共享首选项存储中。我使用共享首选项,因此可以使用Android本身的MultiSelectListPreference。保存值不是问题,但可能是原因。当我尝试读取值时,问题本身就开始了。就在那时,安卓给了Xamarin代码一个ArrayList,Xamarin应该把它转换成一个C#列表。然后我得到了下面的InvalidCastException:

代码语言:javascript
复制
[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)

我使用下面这段代码来读取活动中的值。没什么特别的:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>

这是活动的布局:

代码语言:javascript
复制
<?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>

最后但并非最不重要的是,以下是列表的值:

代码语言:javascript
复制
<?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中的一个bug。所以,如果你找到了一个变通的方法,那也是很好的。

EN

回答 1

Stack Overflow用户

发布于 2014-11-19 23:26:58

您可能遇到了这个Xamarin bug:https://bugzilla.xamarin.com/show_bug.cgi?id=13141,它看起来在一月份就被标记为已修复。

(但请参阅Xamarin论坛上的最新讨论):http://forums.xamarin.com/discussion/6531/preferencemanager-getstringset-keeps-crashing

如果您使用的不是最新的Xamarin版本,那么您可能想尝试一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26905839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档