[英]Why am I getting TimePicker NullPointerException in android 4.0.3?
I have this class which is showing a time picker with a 10 minute interval: 我有一个此类,其中显示的时间选择器的间隔为10分钟:
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
// Convert current minutes to tens
// 55 = 50, 56 = 00
int minute = c.get(Calendar.MINUTE) / 10;
minute = (minute > 5) ? 0 : minute;
final TimePickerDialog tpd = new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
tpd.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
int tpLayoutId = getResources().getIdentifier("timePickerLayout", "id", "android");
ViewGroup tpLayout = (ViewGroup) tpd.findViewById(tpLayoutId);
ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);
// Customize minute NumberPicker
NumberPicker minutePicker = (NumberPicker) layout.getChildAt(2);
minutePicker.setDisplayedValues(new String[]{"00", "10", "20", "30", "40", "50"});
minutePicker.setMinValue(0);
minutePicker.setMaxValue(5);
}
});
return tpd;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
minute = minute * 10;
// Do something with the time chosen by the user
final String selectedDate = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
selectedTimeTxt.setText(selectedDate);
}
}
The code is perfectly working on android Lollipop
, but when I run it on emulator, or real device running android 4.0.3
it throws the following exception: 该代码在android
Lollipop
上完美运行,但是当我在模拟器或运行android 4.0.3
真实设备上运行它时,抛出以下异常:
java.lang.NullPointerException
regarding this line: ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);
关于此行的
java.lang.NullPointerException
: ViewGroup layout = (ViewGroup) tpLayout.getChildAt(0);
The error is more than clear, but I can't see the layout component IDs, since the screen capturing in the Device monitor
is not supported in API 15. 该错误已完全清除,但我看不到布局组件ID,因为API 15不支持“
Device monitor
的屏幕捕获。
Can you give me a push? 你能给我推吗? Why I'm getting this error?
为什么我会收到此错误? What's the layout ID im missing?
我缺少什么布局ID? I'm pretty sure that many developers faced the same issue.
我敢肯定,许多开发人员都会遇到同样的问题。 I know that I'm missing a really small part here, but I'm not able to spot it.
我知道我在这里缺少一小部分,但是我无法发现它。
Actually the problem is here: 实际上问题出在这里:
int tpLayoutId = getResources().getIdentifier("timePickerLayout", "id", "android");
There's no internal identifier with the name timePickerLayout
available in API-15 . API-15中没有名称为
timePickerLayout
内部标识符。
Hence tpLayoutId
is 0
which makes your tpLayout
equals to null
; 因此
tpLayoutId
为0
,这使您的tpLayout
等于null
; and calling a method on null object throws NullPointerException
. 在null对象上调用方法会抛出
NullPointerException
。
Recommendation: don't rely on android's internal resource declarations, as there's a reason that they have been made private. 建议:不要依赖android的内部资源声明,因为有理由将它们设为私有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.