切换按钮 - OnCheckedChangeListener,保存首选项总是在 NULL 上出错
Toggle Button - OnCheckedChangeListener, Saving preferences always errors on NULL
我有一个切换按钮,按预期工作。
为了测试我有一条 Toast 消息,它们都在工作。
但是我尝试将 Toggle 的值存储到共享首选项,我总是收到有关 Null 对象的错误?
我在我的整个应用程序中都使用了这个 SharedPreference 代码,而且我从来没有遇到过问题。
但这是我第一次尝试使用 Toggle,其他一切都是按钮、收音机或滑块。
我错过了什么吗?
如果我注释掉 PreferenceManager Store 值,它工作正常。
这是我的 Toggle 的代码:
Switch myToggleButton = inflated.findViewById(R.id.switchon);
myToggleButton.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked) {
preferenceManager.storeValue(packageName, true);
Toast.makeText(context, "You have Enabled "+ appName + " Notification", Toast.LENGTH_SHORT).show(); }
else{
preferenceManager.storeValueapp(packageName, false);
Toast.makeText(context, "You have DISABLED "+ appName +" Notification", Toast.LENGTH_SHORT).show();
}
}
});
我还尝试通过 运行 双重检查 null:
if (myToggleButton!=null){ //run code here }
但这还是一样
这是我在按下 Toggle On 时遇到的错误。
2020-05-14 12:08:35.360 23783-23783/com.enigamcodes.cutoutnotification E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.enigamcodes.cutoutnotification, PID: 23783
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.enigamcodes.cutoutnotification.Utils.PreferenceManager.storeValue(java.lang.String, java.lang.Object)' on a null object reference
at com.enigamcodes.cutoutnotification.Settings.NotificationColor.onCheckedChanged(NotificationColor.java:133)
这条消息
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.enigamcodes.cutoutnotification.Utils.PreferenceManager.storeValue(java.lang.String, java.lang.Object)' on a null object reference
表示preferenceManager
还没有初始化。
我有一个切换按钮,按预期工作。 为了测试我有一条 Toast 消息,它们都在工作。
但是我尝试将 Toggle 的值存储到共享首选项,我总是收到有关 Null 对象的错误?
我在我的整个应用程序中都使用了这个 SharedPreference 代码,而且我从来没有遇到过问题。 但这是我第一次尝试使用 Toggle,其他一切都是按钮、收音机或滑块。
我错过了什么吗? 如果我注释掉 PreferenceManager Store 值,它工作正常。
这是我的 Toggle 的代码:
Switch myToggleButton = inflated.findViewById(R.id.switchon);
myToggleButton.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked) {
preferenceManager.storeValue(packageName, true);
Toast.makeText(context, "You have Enabled "+ appName + " Notification", Toast.LENGTH_SHORT).show(); }
else{
preferenceManager.storeValueapp(packageName, false);
Toast.makeText(context, "You have DISABLED "+ appName +" Notification", Toast.LENGTH_SHORT).show();
}
}
});
我还尝试通过 运行 双重检查 null:
if (myToggleButton!=null){ //run code here }
但这还是一样
这是我在按下 Toggle On 时遇到的错误。
2020-05-14 12:08:35.360 23783-23783/com.enigamcodes.cutoutnotification E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.enigamcodes.cutoutnotification, PID: 23783
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.enigamcodes.cutoutnotification.Utils.PreferenceManager.storeValue(java.lang.String, java.lang.Object)' on a null object reference
at com.enigamcodes.cutoutnotification.Settings.NotificationColor.onCheckedChanged(NotificationColor.java:133)
这条消息
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.enigamcodes.cutoutnotification.Utils.PreferenceManager.storeValue(java.lang.String, java.lang.Object)' on a null object reference
表示preferenceManager
还没有初始化。