Can anyone help why i am getting this exception !!!
01-15 00:15:51.380 24298-24298/creotive.walleta3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at creotive.walleta3.Reminder$5.onDateSet(Reminder.java:290)
at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:199)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:154)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
My code is like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_layout);
editDate=(EditText)findViewById(R.id.edt_date);
setReminderButt = (Button) findViewById(R.id.setReminderButt);
setReminderButt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = new Dialog(Reminder.this);
dialog.setContentView(R.layout.activity_dialog1);
dialog.setTitle("Reminder Alarm");
editTime=(EditText)findViewById(R.id.edt_time);
setButton=(Button)dialog.findViewById(R.id.btn_set);
date=(Button)dialog.findViewById(R.id.date_picker);
time=(Button)dialog.findViewById(R.id.time_picker);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Reminder.this, "Reagon", Toast.LENGTH_SHORT).show();
final Calendar c = Calendar.getInstance();
myYear = c.get(Calendar.YEAR);
myMonth = c.get(Calendar.MONTH);
myDay = c.get(Calendar.DAY_OF_MONTH);
showDialog(ID_DATEPICKER);
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
myHour = c.get(Calendar.HOUR_OF_DAY);
myMinute = c.get(Calendar.MINUTE);
showDialog(ID_TIMEPICKER);
}
});
setButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestCode=(int)System.currentTimeMillis();
try{
msg=editMessage.getText().toString();
}catch (Exception e) {
msg="";
}
dateTime=dateStr.trim()+" "+timeStr.trim();
try {
SimpleDateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm" );
setDate=(Date)formatter.parse(dateTime);
} catch (ParseException e){
}
al=new Alarm();
al.SetAlarm(Reminder.this, msg, requestCode, setDate, dateTime);
Toast.makeText(getApplicationContext(), "Alarm has set", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case ID_DATEPICKER:
return new DatePickerDialog(this,
myDateSetListener,
myYear, myMonth, myDay);
case ID_TIMEPICKER:
return new TimePickerDialog(this,
myTimeSetListener,
myHour, myMinute, false);
default:
return null;
}
}
private DatePickerDialog.OnDateSetListener myDateSetListener
= new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateStr=String.valueOf(dayOfMonth)+"-"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(year);
editDate.setText(dateStr);
}
};
private TimePickerDialog.OnTimeSetListener myTimeSetListener
= new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeStr=String.valueOf(hourOfDay)+":"+String.valueOf(minute);
editTime.setText(timeStr);
}
};
The scenario is like this: I have button setReminder, and when i click it it shows dialog that has ability to set Time and Date of reminder. This dialog also has two button setTime and setDate, and clicking each of them throws this kind of exception. Idk what's happening !!!
P.S ---- Reminder.java:290 is this line of code: editDate.setText(dateStr);
This line here
editTime=(EditText)findViewById(R.id.edt_time);
is telling it to look in the layout you inflated with setContentView()
when you actually need to look in the layout inflated in the Dialog
Change it to
editTime=(EditText) dialog.findViewById(R.id.edt_time);