我试图在时间选择器中显示自定义分钟间隔,所以这是我正在尝试做的事情。
在我的MainActivity中,我有这个方法(点击按钮时调用),它打开了一个d DialogFragment
:
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
以下是TimePickerFragment
:
public class TimePickerFragment extends DialogFragment
implements DurationTimePickDialog.OnTimeSetListener {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = 0;
// Create a new instance of CustomTimePickerDialog and return it
return new CustomTimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String realMinute= "";
switch (minute) {
case 1:
realMinute = "01";
break;
case 2:
realMinute = "02";
break;
case 3:
realMinute = "03";
break;
case 4:
realMinute = "04";
break;
case 5:
realMinute = "05";
break;
case 6:
realMinute = "06";
break;
case 7:
realMinute = "07";
break;
case 8:
realMinute = "08";
break;
case 9:
realMinute = "09";
break;
default:
realMinute = String.valueOf(minute);
break;
}
final String selectedDate = String.valueOf(hourOfDay) + ":" + realMinute;
EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
selectedTimeTxt.setText(selectedDate);
}
}
正如您所看到的,我正在使用名为CustomTimePickerDialog
的自定义类,以便以10分钟的间隔显示对话框。这里的代码是:
public class CustomTimePickerDialog extends TimePickerDialog {
private final static int TIME_PICKER_INTERVAL = 5;
private TimePicker timePicker;
private final OnTimeSetListener callback;
public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
int hourOfDay, int minute, boolean is24HourView) {
super(context, callBack, hourOfDay, minute / TIME_PICKER_INTERVAL,
is24HourView);
this.callback = callBack;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (callback != null && timePicker != null) {
timePicker.clearFocus();
callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
timePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
}
}
@Override
protected void onStop() {
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field timePickerField = classForid.getField("timePicker");
this.timePicker = (TimePicker) findViewById(timePickerField
.getInt(null));
Field field = classForid.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues
.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
问题是我对我这个奇怪的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference
关于此行:mMinuteSpinner.setMinValue(0);
- 位于CustomTimePickerDialog
类。
我知道我在弄乱这里的东西(可能在TimePickerFragment
类),但作为一个Android和java初学者,我无法发现我的错误。
答案 0 :(得分:1)
NumberPicker mMinuteSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
可能会返回null
。
答案 1 :(得分:0)
android对象引用上的android.widget.NumberPicker.setMinValue(int) 所以它说它被调用null对象,这意味着mMinuteSpinner为空肯定JVM不会出错dude只是调试你的代码如果你需要帮助我会帮你调试
NumberPicker mMinuteSpinner =(NumberPicker)timePicker .findViewById(field.getInt(空));
mMinuteSpinner.setMinValue(0);
答案 2 :(得分:0)
LayoutInflater l = getLayoutInflater();
final View v = l.inflate(R.layout.create_alarm_dialog, null);
//get the number pickers
NumberPicker hoursNP = (NumberPicker) v.findViewById(R.id.hours);
这就是我的做法,我猜你需要指定视图,以便知道在哪里查找ID。对不起,我也是android的菜鸟,这是我的第一个stackover流程帖子。我希望这会有所帮助。