空内容在Kotlin中的共享首选项 [英] Null content Shared Preferences in Kotlin

查看:77
本文介绍了空内容在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.experimental E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.go.experimental, PID: 32656
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.go.experimental/com.go.experimental.AccesoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6864)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:184)
    at com.go.experimental.tools.Preferences.<init>(Preferences.kt:39)
    at com.go.experimental.AccesoActivity.<init>(AccesoActivity.kt:26)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:201) 
    at android.app.ActivityThread.main(ActivityThread.java:6864) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

我不知道我在做什么错,这是我的第一个Kotlin应用程序,因此希望您能帮助我看看我的错误.

I don't know what I'm doing wrong, it's my first Kotlin app so I hope you can help me see my mistake.

这是我的共享的偏好设置"类:

Here's my Shared Preferences class:

class Preferences(context: Context) {

  private val SHARED_PREFS = "GO_SHARED_PREFS"
  private val ID = "id"
  private val NAME = "name"
  private val PHONE = "phone"

  private var prefs = context.getSharedPreferences(SHARED_PREFS, 0)

  var id: Int
    get() = prefs!!.getInt(ID.toString(), 0)
    set(value) = prefs!!.edit().putInt(ID, value).apply()

  var name: String?
    get() = prefs!!.getString(NAME, "")
    set(value) = prefs!!.edit().putString(NAME, value).apply()

  var phone: Int
    get() = prefs!!.getInt(PHONE, 0)
    set(value) = prefs!!.edit().putInt(PHONE, value).apply()

这是我在活动"中使用它的方式:

And here is how I use it on my Activity:

var prefs = Preferences(this@AccesoActivity)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_acceso)

    tvTipoCodigo = findViewById(R.id.tv_tipo_acceso)
    tvNumeroTelefono = findViewById(R.id.tv_guest_number)
    tvCodigoNumerico = findViewById(R.id.tv_numeric_code)
    tvFecha = findViewById(R.id.et_activation_code)
    imCodigoQR = findViewById(R.id.iv_qr_code)

    codigoNumerico = generateNumericalNumber()

    saveCode()
}

private fun saveCode(){
    var name: String? = prefs.name
    var phone: Long? = prefs.phone
}

推荐答案

CommonsWare是完全正确的.您还可以使用 lazy 委托.它增加了一些开销,但至少从我的角度来看更容易阅读

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) }

这篇关于空内容在Kotlin中的共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆