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