java – SwitchPreference setOnPreferenceChangeListener not call-ThrowExceptions

Exception or error:

I tried to showing the Toast, but it’s not called when I changed SwitchPreference state.(JAVA)

    SwitchPreference adwareMalware = findPreference("adwareMalware");
    adwareMalware.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
       @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if (adwareMalware.isChecked()) {
                Toast.makeText(getContext(), "Unchecked", Toast.LENGTH_SHORT).show();

                // Checked the switch programmatically
                adwareMalware.setChecked(false);
            } else {
                Toast.makeText(getContext(), "Checked", Toast.LENGTH_SHORT).show();

                url = malware;
                // Unchecked the switch programmatically
                adwareMalware.setChecked(true);
            }
            return true;
        }
    });
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *