我有一个切换按钮,效果正常。为了测试,我有一条Toast消息,并且它们都在工作。
但是我试图将Toggle的值存储为共享首选项,但是我不断收到有关Null对象的错误?
我在整个应用程序中都使用了此SharedPreference代码,但从未遇到过问题。但这是我第一次尝试使用Toggle,其他所有功能都是按钮,单选按钮或滑块。
我想念什么吗?如果我注释掉PreferenceManager商店的值,它就可以正常工作。
这是我的切换代码: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$2.onCheckedChanged(NotificationColor.java:133)