0

I'm trying to use SharedPreferences and I stumbled in a probably stupid error of mine. This's my code (MainActivity class):

private Boolean checkSSID;
private String connectionSSID;
private int leftPort;
private int rightPort;
private int LLUPort;
private int localUDPPort;
private String platformIPAddrStr;

At bottom of onCreate() method:

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);

at the first getInt() call the app crash with this error:

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

The PreferenceScreen layout:

<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>

In SettingsActivity I declared all the public strings used to recall preferences key .. i.e.:

public static final String
        KEY_PREF_CHECK_SSID = "checkSSID";

Where did I fail?

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
weirdgyn
  • 886
  • 16
  • 47

4 Answers4

2

EditTextPreference returns String values, like ListPreference. You need to convert every value extracted from shared preferences from String to int. For example:

platformType  = Integer.valueOf(sharedPref.getString
        (SettingsActivity.KEY_PREF_PLATFORM_TYPE,"0"));
xcesco
  • 4,690
  • 4
  • 34
  • 65
  • I got errors even on leftPort ... not only on this one... this doesn't fix everything (but it's welcome). – weirdgyn Oct 03 '18 at 16:37
  • 1
    EditTextPreferences return String, like ListPreferences. Every field that you are trying to extract, is a string. I modified the answer, just to be more clear. – xcesco Oct 03 '18 at 16:41
  • Looks like this's the solution ... thnx – weirdgyn Oct 03 '18 at 19:29
0

Try to declare your int values like this

public static final int
        KEY_PREF_LEFT_UDP_PORT = 1000;
Hihikomori
  • 950
  • 6
  • 12
  • uh well no... KEY_PREF_LEFT_UDP_PORT and other final variables defined in SettingsActivity are just reminder for 'key' values in preferences.xml... – weirdgyn Oct 03 '18 at 19:17
0

Loading Preferences from XML ressources can only deal with strings (as far as I know). In your scenario this is the @string:

<android.support.v7.preference.SwitchPreferenceCompat
    android:defaultValue="true"
    android:key="checkSSID"
    android:summary="@string/checkSSID_summary"
    android:title="@string/checkSSID_title" />

Hence I don't know the use case for the other methods like getInt, but I also ended up loading it as string and then parsing/casting it to the required type like describe by @xcesco.

TeddybearCrisis
  • 269
  • 1
  • 9
  • oh right, my bad. Did you try putting the *int*s into a distinct integer resource xml as described here? https://stackoverflow.com/questions/19297522/android-integer-from-xml-resource – TeddybearCrisis Oct 03 '18 at 20:24
  • yes I know but as you can read in xcesco answer the terminal problem is the TextView that will only output String objects – weirdgyn Oct 04 '18 at 06:11
0

looks like all the keys stored in shared preferences are strings , try to load them all as strings then parse the ones you need as integer. like this :

String leftPort_string = sharedPref.getString
    (SettingsActivity.KEY_PREF_LEFT_UDP_PORT,"4001");

leftPort=Integer.parseInt(leftPort_string);
Houcine
  • 101
  • 8