目前我正在尝试在Android的共享偏好设置存储中保存字符串列表。我使用共享首选项,因此可以使用Android本身的MultiSelectListPreference。保存价值不是问题,但可能是原因。当我尝试读取值时,问题本身就开始了。就在那时,Android给Xamarin代码提供了一个ArrayList,而Xamarin应该将它转换为C#List。然后我得到以下InvalidCastException:
[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)
我使用以下代码来读取Activity中的值。没什么特别的:
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中的一个错误。所以,如果你找到一个很好的解决办法。
答案 0 :(得分:0)
您可能遇到过这个Xamarin错误: https://bugzilla.xamarin.com/show_bug.cgi?id=13141 看起来它在1月被标记为固定。
(但请参阅Xamarin论坛中此讨论的最新内容): http://forums.xamarin.com/discussion/6531/preferencemanager-getstringset-keeps-crashing
如果您没有使用最新的Xamarin版本,您可能需要尝试。