Null content Shared Preferences in Kotlin

☆樱花仙子☆ 提交于 2021-01-29 20:26:18

问题


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) 

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
}

回答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:

lateinit var prefs: Preferences

and add this to onCreate() after super.onCreate():

prefs = Preferences(this)



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


来源:https://stackoverflow.com/questions/62173450/null-content-shared-preferences-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!