无法解决符号“ setOnPreferenceClickListener”错误?

时间:2019-08-05 14:20:56

标签: java android android-preferences

我遇到几个无法解析符号错误。还说我的return true语句在方法之外。我已经检查了缩进,并尝试import androidx.preference.Preference.OnPreferenceClickListener;  但从未使用过导入。请帮忙 在setOnPreference clicklistener上显示红色,在onPreferenceClick中显示首选项参数。

public class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }

    Preference cache = (Preference) findPreference(getString(R.string.clear_cache));

    cache.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            //open browser or intent here
            Toast.makeText(requireContext(), "Test!", 
            Toast.LENGTH_LONG).show();
                return true;
        }
    });

    Preference logout = (Preference) findPreference(getString(R.string.logout));

    logout.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            //open browser or intent here
            Intent intent = new Intent(getActivity(), 
            LoginActivity.class);
            startActivity(intent);
            return true;
        }
    });

}

0 个答案:

没有答案