I want to implement a TimePreference using DialogPreference, I found a good tutorial to do that here after implementing the TimePreference I noticed the onSetInitialValue(bool,Object) was deprecated and I implemented instead onSetInitialValue(Object)
When I try to implement onSetInitialValue(object) I'm getting a null pointer exception because defaultValue is null, Although everything is working properly with the deprecated function
fun setTime(time: Int) {
mTime = time
// Save to SharedPreference
persistInt(time)
}
//old version - works
override fun onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any?) {
setTime(
if (restorePersistedValue)
getPersistedInt(mTime)
else
defaultValue as Int
)
}
//New function doesn't work
override fun onSetInitialValue(defaultValue: Any?) {
setTime(
if (mTime!=0)
getPersistedInt(mTime)
else
defaultValue as Int
)
}