У меня есть класс, который обрабатывает и сохраняет информацию. Всё сделано на основе SharedPreferences
. От какого класса надо наследоваться, чтобы работал метод getSharedPreferences
? Сам попробовал наследоваться от ContextWrapper
, код представлен ниже. Но получаю
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
class InformationActivity : AppCompatActivity() {
var context:Context = applicationContext
...
light.text = InfSettings(context).getLight().toString()
...
}
class InfSettings(base: Context?) : ContextWrapper(base) {
private var gameProfile: SharedPreferences? = getSharedPreferences("settings_info", MODE_PRIVATE)
private var editorProfile: SharedPreferences.Editor? = null
private val LIGHT = "person_count_light"
private var lightGame: Int = 0
fun setLightGame(lightGame: Int) {
this.lightGame = lightGame
editorProfile = gameProfile!!.edit()
editorProfile!!.putInt(LIGHT, this.lightGame)
editorProfile!!.apply()
}
...
fun getLight(): Int {
return gameProfile!!.getInt(LIGHT, 0)
}
...
}