空对象引用上的 Sharedpreferences Context()
Sharedpreferences Context() on a null object reference
我是共享首选项的新手。我遵循了 youtube 上的教程,他的代码运行良好。但我的代码没有。我正在做的是为 Sharedpreferences 创建一个 class。然后另一个 class 在其上创建 Acions。然后我的第三个 class 访问这些操作并使它们在适配器中工作。这是我的代码,以及我遇到的错误。
PrefConfig
object PrefConfig {
private const val MY_PREFERENCE_NAME = "com.moataz.afternoonhadeeth.utils.helper"
private const val PREF_TOTAL_KEY = "pref_total_key"
fun saveTotalInPref(context: Context, total: Int) {
val pref = context.getSharedPreferences(MY_PREFERENCE_NAME, Context.MODE_PRIVATE)
val editor = pref.edit()
editor.putInt(PREF_TOTAL_KEY, total)
editor.apply()
}
fun loadTotalFromPref(context: Context): Int {
val pref = context.getSharedPreferences(MY_PREFERENCE_NAME, Context.MODE_PRIVATE)
return pref.getInt(PREF_TOTAL_KEY, 0)
}
}
CounterActions
class CounterActions : AppCompatActivity() {
var counter = PrefConfig.loadTotalFromPref(this)
fun displayCounter(buttonCounter: Button) {
buttonCounter.text = counter.toString()
}
fun addCounter(buttonCounter: Button) {
counter++
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
if (buttonCounter.text == 999.toString()) {
counter = 0
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
}
}
fun resetCounter(buttonCounter: Button) {
counter = 0
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
}
}
CounterActions 清单 class
<activity
android:name=".utils.helper.CounterActions"
android:exported="true" />
HomeAdapter 并且我从 CounterActions 调用方法 class
static class CounterViewHolder extends RecyclerView.ViewHolder {
ItemHomeCounterBinding itemHomeCounterBinding;
CounterActions counter = new CounterActions();
CounterViewHolder(@NonNull ItemHomeCounterBinding itemView) {
super(itemView.getRoot());
itemHomeCounterBinding = itemView;
counter.displayCounter(itemHomeCounterBinding.buttonCounter);
}
void setOnClick() {
itemHomeCounterBinding.buttonCounter.setOnClickListener(v -> {
counter.addCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
itemView.setOnClickListener(v -> {
counter.addCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
itemHomeCounterBinding.resetButtonOnClick.setOnClickListener(v -> {
counter.resetCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
}
}
错误代码
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:174)
at com.moataz.afternoonhadeeth.utils.helper.PrefConfig.loadTotalFromPref(PrefConfig.kt:19)
at com.moataz.afternoonhadeeth.utils.helper.CounterActions.<init>(CounterActions.kt:13)
at com.moataz.afternoonhadeeth.ui.adapter.HomeAdapter$CounterViewHolder.<init>(HomeAdapter.java:339)
at com.moataz.afternoonhadeeth.ui.adapter.HomeAdapter.onCreateViewHolder(HomeAdapter.java:88)
您的 CounterActions
未用作 Activity
,因此不应扩展它(删除 : AppCompatActivity()
)
class CounterActions {
同时删除相关清单条目
但是如何在里面得到合适的Context
呢?只需从 buttonCounter
获取,每个 View
都需要 Context
才能创建。而View
放在Activity
里面,其实就是运行(通过system/user交互或者startActivity
方法启动)
class CounterActions {
fun displayCounter(buttonCounter: Button) {
var counter = PrefConfig.loadTotalFromPref(buttonCounter.context)
buttonCounter.text = counter.toString()
}
fun addCounter(buttonCounter: Button) {
var counter = PrefConfig.loadTotalFromPref(buttonCounter.context)
counter++
PrefConfig.saveTotalInPref(buttonCounter.context, counter)
buttonCounter.text = counter.toString()
if (buttonCounter.text == 999.toString()) {
resetCounter(buttonCounter)
}
}
fun resetCounter(buttonCounter: Button) {
counter = 0
PrefConfig.saveTotalInPref(buttonCounter.context, counter)
buttonCounter.text = counter.toString()
}
}
我是共享首选项的新手。我遵循了 youtube 上的教程,他的代码运行良好。但我的代码没有。我正在做的是为 Sharedpreferences 创建一个 class。然后另一个 class 在其上创建 Acions。然后我的第三个 class 访问这些操作并使它们在适配器中工作。这是我的代码,以及我遇到的错误。
PrefConfig
object PrefConfig {
private const val MY_PREFERENCE_NAME = "com.moataz.afternoonhadeeth.utils.helper"
private const val PREF_TOTAL_KEY = "pref_total_key"
fun saveTotalInPref(context: Context, total: Int) {
val pref = context.getSharedPreferences(MY_PREFERENCE_NAME, Context.MODE_PRIVATE)
val editor = pref.edit()
editor.putInt(PREF_TOTAL_KEY, total)
editor.apply()
}
fun loadTotalFromPref(context: Context): Int {
val pref = context.getSharedPreferences(MY_PREFERENCE_NAME, Context.MODE_PRIVATE)
return pref.getInt(PREF_TOTAL_KEY, 0)
}
}
CounterActions
class CounterActions : AppCompatActivity() {
var counter = PrefConfig.loadTotalFromPref(this)
fun displayCounter(buttonCounter: Button) {
buttonCounter.text = counter.toString()
}
fun addCounter(buttonCounter: Button) {
counter++
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
if (buttonCounter.text == 999.toString()) {
counter = 0
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
}
}
fun resetCounter(buttonCounter: Button) {
counter = 0
PrefConfig.saveTotalInPref(applicationContext, counter)
buttonCounter.text = counter.toString()
}
}
CounterActions 清单 class
<activity
android:name=".utils.helper.CounterActions"
android:exported="true" />
HomeAdapter 并且我从 CounterActions 调用方法 class
static class CounterViewHolder extends RecyclerView.ViewHolder {
ItemHomeCounterBinding itemHomeCounterBinding;
CounterActions counter = new CounterActions();
CounterViewHolder(@NonNull ItemHomeCounterBinding itemView) {
super(itemView.getRoot());
itemHomeCounterBinding = itemView;
counter.displayCounter(itemHomeCounterBinding.buttonCounter);
}
void setOnClick() {
itemHomeCounterBinding.buttonCounter.setOnClickListener(v -> {
counter.addCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
itemView.setOnClickListener(v -> {
counter.addCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
itemHomeCounterBinding.resetButtonOnClick.setOnClickListener(v -> {
counter.resetCounter(itemHomeCounterBinding.buttonCounter);
counter.vibrateOnce(itemView.getContext());
});
}
}
错误代码
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:174)
at com.moataz.afternoonhadeeth.utils.helper.PrefConfig.loadTotalFromPref(PrefConfig.kt:19)
at com.moataz.afternoonhadeeth.utils.helper.CounterActions.<init>(CounterActions.kt:13)
at com.moataz.afternoonhadeeth.ui.adapter.HomeAdapter$CounterViewHolder.<init>(HomeAdapter.java:339)
at com.moataz.afternoonhadeeth.ui.adapter.HomeAdapter.onCreateViewHolder(HomeAdapter.java:88)
您的 CounterActions
未用作 Activity
,因此不应扩展它(删除 : AppCompatActivity()
)
class CounterActions {
同时删除相关清单条目
但是如何在里面得到合适的Context
呢?只需从 buttonCounter
获取,每个 View
都需要 Context
才能创建。而View
放在Activity
里面,其实就是运行(通过system/user交互或者startActivity
方法启动)
class CounterActions {
fun displayCounter(buttonCounter: Button) {
var counter = PrefConfig.loadTotalFromPref(buttonCounter.context)
buttonCounter.text = counter.toString()
}
fun addCounter(buttonCounter: Button) {
var counter = PrefConfig.loadTotalFromPref(buttonCounter.context)
counter++
PrefConfig.saveTotalInPref(buttonCounter.context, counter)
buttonCounter.text = counter.toString()
if (buttonCounter.text == 999.toString()) {
resetCounter(buttonCounter)
}
}
fun resetCounter(buttonCounter: Button) {
counter = 0
PrefConfig.saveTotalInPref(buttonCounter.context, counter)
buttonCounter.text = counter.toString()
}
}