我正在尝试使用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";
我哪里失败了?
最佳答案
EditTextPreference
返回String
值,例如ListPreference
。您需要将从共享首选项中提取的每个值从String
转换为int
。例如:
platformType = Integer.valueOf(sharedPref.getString
(SettingsActivity.KEY_PREF_PLATFORM_TYPE,"0"));
关于java - getInt中的Android SharedPreferences异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52631681/