0

So I've created a custom preference using DialogPreference - nothing terribly special; invoked via XML, runs without any major errors:

public class TimePref extends DialogPreference {

    private TimePicker picker;
    private int value;

    private final static int DEFAULT_VALUE = 0;

    public TimePref(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TimePref(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View onCreateDialogView() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;

        picker = new TimePicker(getContext());
        picker.setLayoutParams(layoutParams);

        FrameLayout dialogView = new FrameLayout(getContext());
        dialogView.addView(picker);

        return dialogView;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);

        picker.setIs24HourView(true);
        setTimePicker(getValue());
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            picker.clearFocus();
            int newValue;
            if (Build.VERSION.SDK_INT >= 23 ) {
                newValue = picker.getHour() * 60;
                newValue += picker.getMinute();
            } else {
                newValue = picker.getCurrentHour() * 60;
                newValue += picker.getCurrentMinute();
            }

            if (callChangeListener(newValue)) {
                setValue(newValue);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, DEFAULT_VALUE);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        if (restorePersistedValue) {
            if (defaultValue != null)
                setValue((int) defaultValue);
        } else {
            setValue(getPersistedInt(DEFAULT_VALUE));
        }
    }

    public void setValue(int value) {
        this.value = value;
        persistInt(this.value);
    }

    public int getValue() {
        return this.value;
    }

    private void setTimePicker (int value) {
        if (Build.VERSION.SDK_INT >= 23 ) {
            picker.setHour((int) (value / 60));
            picker.setMinute(value % 60);
        } else {
            picker.setCurrentHour((int) (value / 60));
            picker.setCurrentMinute(value % 60);
        }
    }

}

Problem arises when I want to access it programmatically, for example:

TimePref p = (TimePref) findPreference(prefKey);
p.setValue(value);

Will return p as null and this throw a java.lang.NullPointerException. So the question is how can one access a custom preference programmatically if you cannot 'find' it?

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Gaddo
  • 126
  • 1
  • 5
  • Can you post your log... for value of p – Whats Going On Mar 20 '17 at 05:18
  • Try this [link](http://stackoverflow.com/questions/5365310/creating-a-dialogpreference-from-xml) – Ashish M Mar 20 '17 at 05:23
  • 1
    is it `PreferenceActivity#findPreference` or `PreferenceManager#findPreference` or `PreferenceFragment#findPreference` or `PreferenceGroup#findPreference` ? – pskink Mar 20 '17 at 05:49
  • PreferenceFragment#findPreference – Gaddo Mar 20 '17 at 05:54
  • ok and what simple "step-in" during [debugging session](https://developer.android.com/studio/debug/index.html#breakPoints) say? – pskink Mar 20 '17 at 05:55
  • Just that findPreference returns null (i.e. p == null). And naturally the error occurs when I try to access a method on p. And yes, it's not a typo in prefKey. – Gaddo Mar 20 '17 at 06:02
  • 1
    it is because `PreferenceManager` is null [here](http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/preference/PreferenceFragment.java#343) – pskink Mar 20 '17 at 06:12
  • I should have mentioned that findPreference has no such problems with other, standard prefrences defined in the XML (e.g. EditTextPreference). Digging a bit deeper, the difference appears to be that these are part of PreferenceGroups, while my custom pref is not. According to the [link](https://developer.android.com/reference/android/preference/PreferenceGroup.html#findPreference(java.lang.CharSequence)) docs[/link]: _"this will recursively search for the preference into children that are also PreferenceGroups."_ – Gaddo Mar 20 '17 at 07:40
  • So, if findPreference won't return custom prefs because they're not part of PreferenceGroups, how else can one grab a reference to them? – Gaddo Mar 20 '17 at 07:45

0 Answers0