Null Content Shared Preferences In Kotlin
I'm developing an app that uses Shared Preferences, so I created a class only for this, but when I launch the app I get this error: 2020-06-03 14:51:50.124 32656-32656/com.go.exper
Solution 1:
var prefs = Preferences(this@AccesoActivity)
This is not going to work. Never try to use an Activity
object until after super.onCreate()
, except in very specific situations.
Switch this to:
lateinitvar prefs: Preferences
and add this to onCreate()
after super.onCreate()
:
prefs = Preferences(this)
Solution 2:
CommonsWare is completely right. You also can use lazy
delegate. It adds a little overhead but it's easier to read at least from my point of view
val prefs by lazy { Preferences(this) }
Post a Comment for "Null Content Shared Preferences In Kotlin"