user3605321
user3605321

Reputation: 45

Android DefaultSharedPreferences String instead of Integer

I have a method that reads a string and two integers from the DefaultSharedPreferences.When I attempt to put the values stored in the two integers from DefaultSharedPreferences into other two integers I get an error saying that the two saved integers (DefaultSharedPreferences) are actually strings.Here is the error from LogCat:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:221)

And here is my preferences.xml (where my two int fields are set as android:inputType="number") and the method that tries to read the values:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<EditTextPreference
    android:defaultValue="@string/set_ip_server"
    android:key="ip_server"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="@string/put_ip"
    android:title="@string/set_ip_server" />
<EditTextPreference
    android:inputType="number" 
    android:defaultValue="@string/set_port_server"
    android:key="port_server"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="@string/put_port"
    android:title="@string/set_port_server" />
<EditTextPreference
    android:inputType="number" 
    android:defaultValue="@string/set_port_client"
    android:key="port_android"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="@string/put_port"
    android:title="@string/set_port_client" />

string ip_server;
int port_server;
int port_android;
    public void read_connection_params()
    {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        ip_server=pref.getString("ip_server", "192.168.0.102");
        port_server=pref.getInt("port_server", 1234);
        port_android=pref.getInt("port_android", 37123);        
    }

I have double checked everything related to those two "integers" - port_server and port_android, and I am out of ideas.

edit: I tried Integer.parseInt( preferences.getString("defaultTip", "15")) but i still get the same error.

Upvotes: 0

Views: 208

Answers (3)

user3605321
user3605321

Reputation: 45

At first I tried

port_server=Integer.parseInt(pref.getString("port_server", "1234"));
port_android=Integer.parseInt(pref.getString("port_android", "37123"));

and it failed.

I got it working by using some temporary variables

String port_s=preferinte.getString("port_server", "1234");                                  
String port_a=preferinte.getString("port_android", "37123");                                    
port_server=Integer.parseInt(port_s);
port_android=Integer.parseInt(port_a);

Upvotes: 0

Chris Feist
Chris Feist

Reputation: 1676

Maybe because your default value is a string. Try changing it to an integer resource.

http://developer.android.com/guide/topics/resources/more-resources.html#Integer

Upvotes: 0

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17105

The value saved by an EditTextPreference is String, but you are trying to obtain int under that key here

port_server=pref.getInt("port_server", 1234);
port_android=pref.getInt("port_android", 37123);

Must be

port_server = Integer.parseInt(pref.getString("port_server", "1234"));
port_android = Integer.parseInt(pref.getString("port_android", "37123"));

The same applies to ListPreference, if you're going to use any.

Upvotes: 1

Related Questions