I am using a standard date picker to allow the user to pick a date. After this I will then calculate the days between the current date and the date which was picked.
The problem is when the date picker is fired off the app crashes, even though I had it working before i put in the joda time API. The code I am using for the date picker is:-
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
//get the current date
final Calendar c = Calendar.getInstance();
cYear = c.get(Calendar.YEAR);
cMonth = c.get(Calendar.MONTH);
cDay = c.get(Calendar.DAY_OF_MONTH);
Date past = new Date(cYear, cMonth, cDay); // current Date
Date today = new Date(mYear, mMonth, mDay); // date Choosen by the user
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays();
mDateDisplay.setText(""+days);
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
//updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
I have an updateDisplay() method which is being references but I have commented everything to do with it out because I don’t think I need it for what I am trying to do. For those interested here it is:
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
People asked for the logcat output here is is
03-17 17:47:42.343: E/AndroidRuntime(8122): FATAL EXCEPTION: main
03-17 17:47:42.343: E/AndroidRuntime(8122): java.lang.IllegalArgumentException: current should be >= start and <= end
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.widget.DatePicker.updateDaySpinner(DatePicker.java:364)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.widget.DatePicker.updateSpinners(DatePicker.java:350)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.widget.DatePicker.init(DatePicker.java:346)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.DatePickerDialog.(DatePickerDialog.java:124)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.DatePickerDialog.(DatePickerDialog.java:83)
03-17 17:47:42.343: E/AndroidRuntime(8122): at com.b00348312.workout.WorkoutChoice.onCreateDialog(WorkoutChoice.java:113)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.Activity.onCreateDialog(Activity.java:2513)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.Activity.createDialog(Activity.java:888)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.Activity.showDialog(Activity.java:2588)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.Activity.showDialog(Activity.java:2555)
03-17 17:47:42.343: E/AndroidRuntime(8122): at com.b00348312.workout.WorkoutChoice$2.onClick(WorkoutChoice.java:49)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.view.View.performClick(View.java:2408)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.view.View$PerformClick.run(View.java:8817)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.os.Handler.handleCallback(Handler.java:587)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.os.Looper.loop(Looper.java:144)
03-17 17:47:42.343: E/AndroidRuntime(8122): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-17 17:47:42.343: E/AndroidRuntime(8122): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 17:47:42.343: E/AndroidRuntime(8122): at java.lang.reflect.Method.invoke(Method.java:521)
03-17 17:47:42.343: E/AndroidRuntime(8122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-17 17:47:42.343: E/AndroidRuntime(8122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-17 17:47:42.343: E/AndroidRuntime(8122): at dalvik.system.NativeStart.main(Native Method)
you can see the example here that i have put the date and time picker dialog and set it to the textView
http://typicaljava.blogspot.in/2012/02/date-picker-time-picker-dialog-and-set.html
Hope this might helps you.