0
我得到了一个自定义对话框,其上有一个datepicker
。在我的Fragment
我有一个按钮,当我按下它时,对话框出现,我从datepicker
中选择日期。我希望所选日期在Fragment
的textview
上显示。我的代码如下:Android自定义对话框的Datepicker
这是在对话框中我的主要Activity
持有的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();
}
我得到了日,月和年从datepicker
,并将其设置为我的TextView与
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();
什么即时做错了什么?在此先感谢
NOP,我仍然得到这个错误... – Kzaf
我在'PickedDate.setText得到一个错误(新的StringBuilder() .append(天).append( “ ”) \t \t \t \t \t追加。(“ - ”)追加(月).append(“ - ”)追加(年));' – Kzaf
,我只是发现它,我不必使用promtView在TextView。代码是最终的DatePicker datePicker =(DatePicker)promptView.findViewById(R.id.datePicker1); final TextView PickedDate =(TextView)findViewById(R.id.pickeddate); 谢谢! – Kzaf