skbrhmn skbrhmn - 1 year ago 105
Android Question

App crashes instead of Date Picker dialog popping up

I want the date picker dialog to pop up when an EditText is clicked. I have followed a few SO posts and tutorials but nothing pops up and then the app crashes after a while.

I am not sure if this is the right way to do it or if I am missing something. Any help is appreciated!

My code so far, onCreate:

inputDate = (EditText) findViewById(;
calendar = Calendar.getInstance();

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);


inputDate.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(getApplicationContext(), date, calendar
.get(Calendar.YEAR), calendar.get(Calendar.MONTH),


private void updateLabel() {

String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);



android:hint="mm / dd/ yyyy"

Error logcat:

08-20 17:45:01.082 4892-4892/? E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@224e570
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService =$Stub$Proxy@c3e00e9
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService =$Stub$Proxy@c3e00e9
08-20 17:45:01.122 4892-4892/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bluefirelabs.apptiva, PID: 4892
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(
at android.view.WindowManagerGlobal.addView(
at android.view.WindowManagerImpl.addView(
at com.bluefirelabs.apptiva.UserInfoActivity$7.onClick(
at android.view.View.performClick(
at android.widget.TextView.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: ro.product_ship = true
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: ro.debug_level = 0x4f4c
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: sys.mobilecare.preload = false

Answer Source

Thats mine and it works well:

DatePickerDialogdatePickerDialog = new DatePickerDialog(this,, new DatePickerDialog.OnDateSetListener() {
   public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
      //Magic happens here
}, nowYear, nowMonth, nowDay);;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download