dialog小小感悟
前言
因为最近在做项目,然后遇上困难,虽然在老鸟眼中不是很难,但是我想提醒自己,所以注文把
正文
那天遇上这个错误
代码
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
calendar.set(year, monthOfYear, dayOfMonth);
Toast.makeText(getApplicationContext(), "密码或者帐号错误,请检查后再输入!", Toast.LENGTH_SHORT).show();
flushDate(year, monthOfYear, dayOfMonth, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
}
});
TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);
timePicker.setOnTimeChangedListener(new OnTimeChangedListener()
{
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
flushDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
}
});
错误截图
![](http://my.csdn.net/uploads/201206/04/1338765342_4664.jpg)
错误地方 :DatePicker datePicker=(DatePicker)findViewById(R.id.datePicker);
错误修改:DatePicker datePicker = (DatePicker)textEntryView.findViewById(R.id.datePicker);
错误原因:
因为你要从父亲那里找R.id.datePicker 而它的父亲是R.layout.time也就是final View textEntryView = factory.inflate(R.layout.time, null);
如果你DatePicker datePicker=(DatePicker)findViewById(R.id.datePicker);
它会默认是DatePicker datePicker=(DatePicker)this.findViewById(R.id.datePicker);而this是DailyActivity的context,所以会在R.layout.daily里面找这个控件,而在R.layout.daily里并没有定义这个R.id.datePicker,所以会报空指针异常。