Android-自定义对话框中的日期选择器
我得到了一个自定义对话框,上面有一个日期选择器。在我的Android-自定义对话框中的日期选择器,android,android-fragments,datepicker,android-dialog,android-datepicker,Android,Android Fragments,Datepicker,Android Dialog,Android Datepicker,我得到了一个自定义对话框,上面有一个日期选择器。在我的Fragment上,我得到一个按钮,当我按下它时,对话框显示出来,我从日期选择器中选择日期。我希望所选日期显示在我的片段的文本视图中。我的代码如下: 这是我的主活动上包含片段的对话框的代码: public int day; public int month; public int year; @SuppressLint("InflateParams") @SuppressWarnings("deprecati
Fragment
上,我得到一个按钮,当我按下它时,对话框显示出来,我从日期选择器中选择日期。我希望所选日期显示在我的片段的文本视图中。我的代码如下:
这是我的主活动
上包含片段的对话框的代码
:
public int day;
public int month;
public int year;
@SuppressLint("InflateParams")
@SuppressWarnings("deprecation")
public void SetDate(){
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Select Date");
alertDialog.setIcon(R.drawable.action_reservations);
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.datepicker, null);
alertDialog.setView(promptView);
final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
final TextView PickedDate = (TextView) findViewById(R.id.pickeddate);
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
day=datePicker.getDayOfMonth();
month=datePicker.getMonth() + 1;
year=datePicker.getYear();
PickedDate.setText(new StringBuilder().append(day).append(" ").
append("-").append(month).append("-").append(year));
}
});
// Showing Alert Message
alertDialog.show();
}
我从日期选择器中获取日期、月份和年份,并使用
PickedDate.setText(new StringBuilder().append(day).append(" ").
append("-").append(month).append("-").append(year));
同样在我的片段中,我调用我的对话框,代码如下:
final Button SetDate = (Button)rootView.findViewById(R.id.selectdate);
SetDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((MainActivity) getActivity()).SetDate();
}
});
当我运行我的应用程序时,我在以下几行中得到一个nullPointerException:
day=datePicker.getDayOfMonth();
month=datePicker.getMonth() + 1;
year=datePicker.getYear();
我做错了什么?提前感谢尝试更换
final DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
final TextView PickedDate = (TextView) findViewById(R.id.pickeddate);
用这个
final DatePicker datePicker = (DatePicker) promptView.findViewById(R.id.datePicker1);
final TextView PickedDate = (TextView) promptView.findViewById(R.id.pickeddate);
我在pickeDate.setText(new StringBuilder().append(day).append(“”).append(“”).append(“”.append(“”).append(month.append(“”).append(“”.append(year))中出错代码>我刚刚找到它,我不必在TextView中使用promtView。代码是final DatePicker DatePicker=(DatePicker)promptView.findviewbyd(R.id.datePicker1);最终TextView PickedDate=(TextView)findViewById(R.id.PickedDate);谢谢