我的平台是android4.2.2的我拖出来的DatePicker跟书上的不一样,
public class MainActivity extends Activity {
int year,month,hour,minute,day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatePicker date=(DatePicker)findViewById(R.id.datePicker1);
TimePicker time=(TimePicker)findViewById(R.id.timePicker1);
//获取当前的年月日小时分钟
Calendar c=Calendar.getInstance();
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
minute=c.get(Calendar.MINUTE);
hour=c.get(Calendar.HOUR);
//初始化DatePicker组件,初始化时指定监听器
date.init(year, month, day,new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker arg0, int year, int month, int day) {
// TODO Auto-generated method stub
MainActivity.this.year=year;
MainActivity.this.month=month;
MainActivity.this.day=day;
showDate(year,month,day,hour,minute);
}
});
time.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker arg0, int hour, int minute) {
// TODO Auto-generated method stub
MainActivity.this.hour=hour;
MainActivity.this.minute=minute;
showDate(year,month,day,hour,minute);
}
});
}
private void showDate(int year2, int month2, int day2, int hour2,
int minute2) {
// TODO Auto-generated method stub
EditText text=(EditText)findViewById(R.id.show);
text.setText("所选时间为:"+year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
根据书上我写的代码发现只要点击一下两个控件就会自动退出模拟机的程序,想知道为什么,怎么改
------解决方案--------------------
final Calendar calendar = Calendar.getInstance();
DatePickerDialog d = new DatePickerDialog(
getActivity(),
this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));