急急急急急!
为什么我的代码和老师的一样,结果运行的时候直接崩了
为什么我的代码和老师的一样,结果运行的时候直接崩了
2016-07-25
public class MainActivity extends Activity {
private DatePicker datepicker;
private TimePicker timepicker;
private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取一个日历对象
cal.getInstance();
//获取年月日时分秒的信息
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH)+1;
day=cal.get(Calendar.DAY_OF_MONTH);
hour=cal.get(Calendar.HOUR_OF_DAY);
minute=cal.get(Calendar.MINUTE);
//标题式
setTitle(year+"-"+month+"-"+day+"-"+hour+":"+minute);
//初始化控件
datepicker=(DatePicker) findViewById(R.id.datePicker1);
timepicker=(TimePicker) findViewById(R.id.timePicker1);
//datepicker初始化(第四个为事件监听器)
datepicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthofyear, int dayofmonth) {
setTitle(year+"-"+(monthofyear+1)+"-"+dayofmonth+"-"+hour+":"+minute);
}
});
}
}
举报