gpt4 book ai didi

java - 为什么我会收到空​​对象引用错误?

转载 作者:行者123 更新时间:2023-12-01 11:56:54 25 4
gpt4 key购买 nike

我试图在时间选择器中显示自定义的分钟间隔,所以这就是我想要做的。在我的 MainActivity 中,我有这个方法(单击按钮时调用),它打开一个 dDialogFragment:

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:尝试在空对象引用上调用虚拟方法“void android.widget.NumberPicker.setMinValue(int)” 关于此行:mMinuteSpinner.setMinValue( 0); - 在 CustomTimePickerDialog 类中。

我知道我把这里的事情弄乱了(可能是在 TimePickerFragment 类中),但作为一个 android 和 java 初学者,我无法发现我的错误。

你能给我一个线索吗?

最佳答案

NumberPicker mMinuteSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));

可能返回null

关于java - 为什么我会收到空​​对象引用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com