[英]Problems using Android TimePicker.getCurrentHour
I'm making an app that utilizes a TimePicker, and this is my first time using it. 我正在制作一个使用TimePicker的应用程序,这是我第一次使用它。 My code is as follows:
我的代码如下:
final Dialog mTimeDialog = new Dialog(this);
TimePicker timePicker = (TimePicker)mTimeDialog.findViewById(R.id.timePicker);
TimePicker timePicker1 = (TimePicker)mTimeDialog.findViewById(R.id.timePicker1);
int starthour = timePicker.getCurrentHour();
int startminute = timePicker.getCurrentMinute();
int finishhour = timePicker1.getCurrentHour();
int finishminute = timePicker1.getCurrentMinute();
But I'm getting an exception when I try to run it. 但是,当我尝试运行它时出现了异常。 When I push the save button that triggers the
onClick
that this is contained in, I get this: 当我按下保存按钮触发包含它的
onClick
时,得到以下信息:
Caused by: java.lang.NullPointerException
at com.csam.hourtracker.hourentry.onClick(hourentry.java:40)
Any help would be great! 任何帮助将是巨大的!
You have the below 你有以下
final Dialog mTimeDialog = new Dialog(this);
TimePicker timePicker = (TimePicker)mTimeDialog.findViewById(R.id.timePicker);
You have not set the content of layout to Dialog before initializing the TimerPicker's 在初始化TimerPicker的布局之前,尚未将布局的内容设置为Dialog。
You are missing 你不见了
mTimeDialog.setContentView(yourview);
Hence your timepicker's are null causing NPE. 因此,您的时间选择器为null会导致NPE。
findViewById
looks for a view with the id mentioned in the current infalted layout. findViewById
查找具有当前infalted布局中提到的ID的视图。 I believe you have a custom layout for your dialog. 我相信您的对话框具有自定义布局。 You need to inflate the layout first and then initialize your views.
您需要先膨胀布局,然后初始化视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.