0

I always get this error java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

  val sharedPreferences = getSharedPreferences("prefs", MODE_PRIVATE)
  val editor = sharedPreferences.edit()
  var myLongitude = 36.892672
  editor.putDouble("longitude", myLongitude)
  editor.apply()

The error is from here:

val savedLongitude : Double = sharedPreferences.getDouble("longitude", 0.0)

I have these methods:

 fun SharedPreferences.Editor.putDouble(key: String, double: Double) =
          putLong(key, java.lang.Double.doubleToRawLongBits(double))

  fun SharedPreferences.getDouble(key: String, default: Double) =
          java.lang.Double.longBitsToDouble(getLong(key, java.lang.Double.doubleToRawLongBits(default)))

My goal is to save a double value to SharedPref and then retrieve it

Bandy
  • 161
  • 12
  • you save and retrieve value with different keys – Alex Rmcf Dec 18 '20 at 11:24
  • also, i do not know how you do this in kotlin, but in java you suppose to call .commit after putString(key, value) – Alex Rmcf Dec 18 '20 at 11:25
  • sorry i edited the quuestion – Bandy Dec 18 '20 at 11:26
  • 1
    ok, what about String.valueOf(double) instead double.toString? – Alex Rmcf Dec 18 '20 at 11:29
  • yes I tried that before and i still get the same error – Bandy Dec 18 '20 at 11:36
  • Any reason why you're not using `SharedPreferences.putFloat()` ? Do you really need that precision here? – Furkan Yurdakul Dec 18 '20 at 13:49
  • Your code just changed from reading ``String``s to whatever Eugene Troyanskii posted in their answer. Whatever you're actually doing, you've stored an ``Int`` with that key and now you're trying to read the value back as something else – cactustictacs Dec 18 '20 at 13:53
  • If you're trying out different things then ``remove`` the current value from the ``SharedPreferences``, if you're always reading it before you write a new one then you're never going to update it because you hit the read exception first – cactustictacs Dec 18 '20 at 13:57
  • I am not using an int anywhere – Bandy Dec 18 '20 at 14:25
  • you haven't posted your stacktrace, but assuming you're getting that exception when you're reading a value from the ``SharedPreferences``, that means your key is pulling a stored ``Int`` and trying to cast it to a ``String`` or ``Long`` or whatever. Which means an ``Int`` was stored with that key at some point, which is why I'm saying to do a ``remove()`` call to clear it away (just running the app with it once is enough, then you can delete it from your code) – cactustictacs Dec 18 '20 at 21:38

2 Answers2

0

You can use much elegant approach. https://stackoverflow.com/a/18098090/5422725 this is variant for java. But in case you use kotlin, you could use extension to make it mor eclear.

Kotlin extension way (much more pretty than using weird utils classes or whatever)

fun SharedPreferences.Editor.putDouble(key: String, double: Double) =
    putLong(key, java.lang.Double.doubleToRawLongBits(double))

fun SharedPreferences.getDouble(key: String, default: Double) =
    java.lang.Double.longBitsToDouble(getLong(key, java.lang.Double.doubleToRawLongBits(default)))

enter link description here

  • Yes, i have seen this before but i get a similar error: `java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long` – Bandy Dec 18 '20 at 12:08
0

do this to store double value to preferences

 final Editor editor = getPreferences().edit();
 editor.putLong(key, Double.doubleToRawLongBits(value));
 editor.apply();

Double.doubleToRawLongBits() is

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.

To get double vlaues from preferences use this

Double.longBitsToDouble(getPreferences().getLong(key, Double.doubleToLongBits(defValue)));
Priyanka
  • 3,369
  • 1
  • 10
  • 33