0

Can someone help me understand why am I having an error when I use an int shared preference but when I use String it works fine?

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int text = getPrefs.getInt("score", 3);
textView1.setText(text)

EDITED

Here's my logcat

        10-18 20:20:56.255: D/dalvikvm(6245): GC_FOR_MALLOC freed 2597 objects / 165960 bytes in 120ms
        10-18 20:20:56.625: D/AndroidRuntime(6245): Shutting down VM
        10-18 20:20:56.625: W/dalvikvm(6245): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
        10-18 20:20:56.647: E/AndroidRuntime(6245): FATAL EXCEPTION: main
        10-18 20:20:56.647: E/AndroidRuntime(6245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.logipic/com.thesis.logipic.Gameplay}: java.lang.ClassCastException: java.lang.String
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.os.Handler.dispatchMessage(Handler.java:99)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.os.Looper.loop(Looper.java:123)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread.main(ActivityThread.java:4627)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at java.lang.reflect.Method.invokeNative(Native Method)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at java.lang.reflect.Method.invoke(Method.java:521)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at dalvik.system.NativeStart.main(Native Method)
        10-18 20:20:56.647: E/AndroidRuntime(6245): Caused by: java.lang.ClassCastException: java.lang.String
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at com.thesis.logipic.Gameplay.onCreate(Gameplay.java:157)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
        10-18 20:20:56.647: E/AndroidRuntime(6245):     ... 11 more
Aldy
  • 83
  • 1
  • 10

4 Answers4

2

Set your textview text as follows:

textView1.setText(Integer.toString(text));

If you are only passing a int as parameter to a function that expects a String (or CharSequence in this case), you need to specify the conversion to String, otherwise the compiler doesn't know whether you are trying to use it as a String or if there should be a function setText() that expects int as the parameter.

If you combine your int with a String, like setText("My value is: " + text); then you don't need the conversion because the compiler can safely assume you mean to concatenate your int with the specified String.

Ricardo
  • 7,785
  • 8
  • 40
  • 60
  • I think you are confused about `parseInt`. – C. Ross Nov 23 '13 at 17:38
  • tried it but still gives me an error when I open the activity containing the code – Aldy Nov 23 '13 at 17:50
  • You need to debug your code and try to find out what exactly is happening. Then post your question with the details of what you are trying to do and of your error. See here for tips on how to debug: http://stackoverflow.com/questions/8551818/how-to-debug-android-application-line-by-line-using-eclipse – Ricardo Nov 23 '13 at 17:55
0

You're passing an int to setText which requires a CharSequence, usually a String. This will not compile.

You need to convert your int to a String, usually through Integer.toString(myInt);


The runtime error you are experiencing is because the underlying value is defined as a String. Either you are populating it elsewhere as a String, possibly using setString, or defaulting it as a String.

See also: SharedPreferences.Editor.putInt

C. Ross
  • 31,137
  • 42
  • 147
  • 238
0

Change this:

textView1.setText(text);

To this:

textView1.setText(String.valueOf(text));
Devrim
  • 15,345
  • 4
  • 66
  • 74
0

Write this

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    int text = getPrefs.getInt("score", 3);
    textView1.setText(Integer.toString(text));
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70