所以我使用DialogPreference创建了一个自定义首选项 - 没什么特别的;通过XML调用,运行没有任何重大错误:
#posts-wrap {
padding: 20px;
text-align: center;
}
ul.medium-grid {
margin: 0 auto;
display: inline-block;
padding: 0;
text-align: center;
}
#equipment-gallery li {
float: none;
list-style: none;
margin: 20px;
display: inline-block;
}
当我想以编程方式访问它时出现问题,例如:
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);
}
}
}
将p返回null并抛出java.lang.NullPointerException。所以问题是,如果你无法“找到”它,如何以编程方式访问自定义首选项?