skbrhmn
skbrhmn

Reputation: 1199

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, Activity.java onCreate:

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

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

        @Override
        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);
            updateLabel();
        }

    };

    inputDate.setOnClickListener(new OnClickListener() {

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

updateLabel:

private void updateLabel() {

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

    inputDate.setText(sdf.format(calendar.getTime()));
}

xml:

<EditText
                    android:id="@+id/inputDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="mm / dd/ yyyy"
                    android:imeOptions="actionDone"
                    android:focusable="false"
                    />

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 = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c3e00e9
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$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(ViewRootImpl.java:857)
                                                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
                                                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                                                     at android.app.Dialog.show(Dialog.java:350)
                                                     at com.bluefirelabs.apptiva.UserInfoActivity$7.onClick(UserInfoActivity.java:125)
                                                     at android.view.View.performClick(View.java:5702)
                                                     at android.widget.TextView.performClick(TextView.java:10885)
                                                     at android.view.View$PerformClick.run(View.java:22533)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:158)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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

Upvotes: 0

Views: 1023

Answers (1)

XxGoliathusxX
XxGoliathusxX

Reputation: 982

Thats mine and it works well:

DatePickerDialogdatePickerDialog = new DatePickerDialog(this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
      //Magic happens here
   }
}, nowYear, nowMonth, nowDay);
datePickerDialog.show();

Upvotes: 1

Related Questions