当前位置:首页 > 技术心得 > 正文内容

Android MultiSelectListPreference, java.lang.String cannot be cast to java.util.Set

xjtudll9年前 (2017-06-26)技术心得8080

问题及现象:

尝试添加一个 MultiSelectListPreference到设置界面,MultiSelectListPreference基本代码如下:

<MultiSelectListPreference

android:title="@string/title"

android:summary="@string/message"

android:key="pref_foobar"

android:defaultValue="@array/empty_array"

android:entries="@array/fooArray"

android:entryValues="@array/fooValues" />


array如下:

<resources>

<string-array name="fooArray">

<item>Foo</item>

<item>Bar</item>

</string-array>

<string-array name="fooValues">

<item>foo_value</item>

<item>bar_value</item>

</string-array>

<string-array name="empty_array"/>

</resources>


编译后警告如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject/.SettingsActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Set

以下是Activity的代码:

public class SettingsActivity extends Activity {
private static final String TAG_SETTINGS_FRAGMENT = "SETTINGS_FRAGMENT";
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

if (savedInstanceState == null) {            getFragmentManager().beginTransaction() .replace(android.R.id.content, new PreferenceFragment(), TAG_SETTINGS_FRAGMENT).commit(); } }

public static class PreferenceFragment extends android.preference.PreferenceFragment{

public PreferenceFragment() { //required }

@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);            addPreferencesFromResource(R.xml.preferences); } }}

 


 

原因:

某个Key的属性之前是String,后来改成了Set,且在调试的过程中曾经存储过数据,所以才会冲突。

It feels like you already have a plain String preference under this key on your device or emulator. Try completely uninstalling and reinstalling the app, or using "Clear Data" for the app in its screen in Settings, to wipe out your existing SharedPreferences.


解决办法:

1、清除该APP数据;

2、卸载APP再重装;

 

参考资料:http://stackoverflow.com/questions/26179178/android-multiselectlistpreference-java-lang-string-cannot-be-cast-to-java-util

扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://www.xjtudll.cn/Exp/459/

标签: Android
分享给朋友:

“Android MultiSelectListPreference, java.lang.String cannot be cast to java.util.Set” 的相关文章

浅析电波表——德国DCF码

浅析电波表——德国DCF码

参考文献:   http://en.wikipedia.org/wiki/DCF77 http://www.ptb.de/en/publikationen/news/html/news032/artikel/03207.htm http://www.ptb.de/cms/index.php...

MSP430 fail to initialize device解决

MSP430 fail to initialize device解决

日前,在用MSP430 USB仿真器下载程序的时候,突然提示“fail to initialize device”,碰到这个问题,我的第一反应就是检查下载线与单片机是否连接正确,仔细检查了一遍连线,发现连接无误,但是就是每次都提示“fail to initialize device”,然后无法...

提交到app store时,提示ERROR ITMS-90478 ,ERROR ITMS-90062错误

提交到app store时,提示ERROR ITMS-90478 ,ERROR ITMS-90062错误

ios app提交到AppStore时,提示ERROR ITMS-90478 ,ERROR ITMS-90062,如下图所示: 看字面意思,应该是我提交的版本比之前上架的版本要低。 但是,之前的版本是V1.03,现在是V1.1.0。好像版本要高,并没有低。 然而,到AppStore构建版本那里查看...

SQL Server 数据类型xtype

xtype=34 'image' xtype= 35 'text' xtype=36 'uniqueidentifier' xtype=48 'tinyint' xtype=52 'smallint' xtype=56 'int' xtype=58 's...

金蝶 老单增加工具栏按钮(Raise Event)

Public Sub AddToolButton(name As String, Caption As String, Description As String, Optional imagefilename As String = '', Optional iOrder As Long = 47...

ios:autolayout下如何获取UIView的实际宽度

参考资料: http://stackoverflow.com/questions/27653247/how-to-get-real-size-uiview-with-autolayout 问题: 在storyboard里放了一个UIView,并使用autolayout来约束其宽度。为了获取其实际宽度...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。