【问题标题】:Kotlin Android Shared Preferences - lateinit property prefs has not been initializedKotlin Android Shared Preferences - lateinit 属性首选项尚未初始化
【发布时间】:2018-07-11 22:30:38
【问题描述】:

我尝试关注this tutorial,但尝试为我的 Sh.Preference (prefs.token = "sometoken") 赋值时出错:

kotlin.UninitializedPropertyAccessException: lateinit property prefs has not been initialized

我不明白错误在哪里,我还检查了this thread。 这是我的代码sn-ps

Prefs.kt:

class Prefs(context: Context) {
    private val PREFS_FILENAME = "com.example.myapp.prefs"
    private val PREFS_TOKEN = "token"
    private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0)

    var token: String?
        get() = prefs.getString(PREFS_TOKEN, "")
        set(value) = prefs.edit().putString(PREFS_TOKEN, value).apply()
}

App.kt:

val prefs: Prefs by lazy {
    App.prefs
}

class App : Application() {
    companion object {
        lateinit var prefs: Prefs
    }

    override fun onCreate() {
        prefs = Prefs(applicationContext)
        super.onCreate()
    }
}

prefs.token有一个默认值"",那为什么日志说没有初始化呢?

【问题讨论】:

  • 我不会以静态方式保存共享首选项,因为它引用了上下文。
  • 尝试在 onCreate() 中加入 print() 语句,以验证在访问全局 prefs 变量之前它确实被调用。
  • 你在哪里做的prefs.token = "sometoken"?
  • 您的代码令人困惑。您有两个名为 prefs 的不同属性。一次是 by lazy val,另一个是 lateinit var。此外,问题不是令牌,而是偏好。
  • 您是否尝试从伴随对象块中删除 lateinit var prefs: Prefs,将其放在外面......这可能会有所帮助

标签: android kotlin sharedpreferences


【解决方案1】:

好的,问题找到了...代码没问题,我只是错过了添加这一行

android:name=".App"

在标签中 <application 在我的 Android 清单中。

【讨论】:

    【解决方案2】:

    就我而言,我只是在 onCreate() 中初始化 SharedPreference,一切正常

    例如:MainActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            AppPreferences.init(this) // added this
    }
    

    SharedPreferences 对象:

        object AppPreferences {
            private const val NAME = "SpinKotlin"
            private const val MODE = Context.MODE_PRIVATE
            private lateinit var preferences: SharedPreferences
        
            // list of app specific preferences
            private val IS_FIRST_RUN_PREF = Pair("is_first_run", false)
        
            fun init(context: Context) {
                preferences = context.getSharedPreferences(NAME, MODE)
            }
        
            /**
             * SharedPreferences extension function, so we won't need to call edit() and apply()
             * ourselves on every SharedPreferences operation.
             */
            private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
                val editor = edit()
                operation(editor)
                editor.apply()
            }
    
            // getter and setter with Shared Preference     
            var temperature: Float
                // custom getter to get a preference of a desired type, with a predefined default value
                get() = preferences.getFloat("temp",1f )
        
                // custom setter to save a preference back to preferences file
                set(value) = preferences.edit {
                    it.putFloat("temp", value)
                }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-22
      • 2020-04-14
      • 2021-10-13
      • 2022-07-05
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2020-09-23
      相关资源
      最近更新 更多