我正在尝试使用 SharedPreferences ,但我偶然发现了一个可能是我的愚蠢错误。这是我的代码( MainActivity 类):
private Boolean checkSSID;
private String connectionSSID;
private int leftPort;
private int rightPort;
private int LLUPort;
private int localUDPPort;
private String platformIPAddrStr;
在 onCreate()方法的底部:
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
checkSSID = sharedPref.getBoolean
(SettingsActivity.KEY_PREF_CHECK_SSID, false);
connectionSSID = sharedPref.getString
(SettingsActivity.KEY_PREF_CONNECTION_SSID, "\"bulldog\"");
leftPort = sharedPref.getInt
(SettingsActivity.KEY_PREF_LEFT_UDP_PORT, 4001);
rightPort = sharedPref.getInt
(SettingsActivity.KEY_PREF_RIGHT_UDP_PORT, 4002);
LLUPort = sharedPref.getInt
(SettingsActivity.KEY_PREF_LLU_UDP_PORT, 8888);
localUDPPort = sharedPref.getInt
(SettingsActivity.KEY_PREF_LOCAL_UDP_PORT, 5555);
platformType = sharedPref.getInt
(SettingsActivity.KEY_PREF_PLATFORM_TYPE,0);
在第一个 getInt()出现此错误时调用应用程序崩溃:
java.lang.RuntimeException: Unable to start activity ComponentInfo{it.xxx/it.xxx.MainActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
PreferenceScreen布局:
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="checkSSID"
android:summary="@string/checkSSID_summary"
android:title="@string/checkSSID_title" />
<EditTextPreference
android:defaultValue="bulldog"
android:key="connectionSSID"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/LLUUDPPort_summary"
android:title="@string/LLUUDPPort_title" />
<EditTextPreference
android:defaultValue="192.168.1.40"
android:key="platformIPAddr"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/PlatformIPAddr_summary"
android:title="@string/PlatformIPAddr_title" />
<ListPreference
android:defaultValue="1"
android:entries="@array/PlatformType_titles"
android:entryValues="@array/PlatformType_values"
android:key="platformType"
android:summary="@string/PlatformType_summary"
android:title="@string/PlatformType_title" />
<EditTextPreference
android:defaultValue="4001"
android:key="leftBankUDPPort"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/LeftBankUDPPort_summary"
android:title="@string/LeftBankUDPPort_title" />
<EditTextPreference
android:defaultValue="4002"
android:key="rightBankUDPPort"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/RightBankUDPPort_summary"
android:title="@string/RightBankUDPPort_title" />
<EditTextPreference
android:defaultValue="8888"
android:key="LLUUDPPort"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/LLUUDPPort_summary"
android:title="@string/LLUUDPPort_title" />
<EditTextPreference
android:defaultValue="5555"
android:key="localUDPPort"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/localUDPPort_summary"
android:title="@string/localUDPPort_title" />
</PreferenceScreen>
在 SettingsActivity 中,我声明了用于调用偏好键的所有公共字符串。即:
public static final String
KEY_PREF_CHECK_SSID = "checkSSID";
我在哪里失败了?
答案 0 :(得分:2)
EditTextPreference
返回String
值,例如ListPreference
。您需要将从共享首选项中提取的每个值从String
转换为int
。例如:
platformType = Integer.valueOf(sharedPref.getString
(SettingsActivity.KEY_PREF_PLATFORM_TYPE,"0"));
答案 1 :(得分:0)
尝试这样声明您的int值
public static final int
KEY_PREF_LEFT_UDP_PORT = 1000;
答案 2 :(得分:0)
从XML资源加载首选项只能处理字符串(据我所知)。在您的情况下,这是@string:
<android.support.v7.preference.SwitchPreferenceCompat
android:defaultValue="true"
android:key="checkSSID"
android:summary="@string/checkSSID_summary"
android:title="@string/checkSSID_title" />
因此,我不知道其他方法(例如 getInt )的用例,但我最终也将其作为字符串加载,然后将其解析/广播为所需的类型,如@xcesco所描述
答案 3 :(得分:0)
似乎共享首选项中存储的所有键都是字符串,请尝试将它们全部加载为字符串,然后将所需的键解析为整数。 像这样:
var a ={
"House 1": {
"Room 1": [
{"1": "TV"},
{"2": "Sofa"},
],
"Room 2": [
{"3": "Desk"},
{"4": "Sink"},
]
},
"House 2": {
"Room 1": [
{"5": "Chair"},
{"6": "Iron"},
],
"Room 2": [
{"7": "Safe"},
{"8": "Cupboard"},
]
}
};
var search=4;
Object.keys(a).reduce((ans,houseIn)=>{
return ans||Object.keys(a[houseIn]).reduce((ans,roomIn)=>{
return ans|| a[houseIn][roomIn].reduce((ans,item,itemIn)=>{
if(parseInt(Object.keys(item)[0])==search){
console.log("found it! " + a[houseIn][roomIn][itemIn][Object.keys(item)[0]]);
return true;
}else{
return false;
}
},false);
},false);
},false);