1

i have a datepicker setup and it keeps crashing the application. i followed this Tutorial and i don't know why it is not working.

i will post the full code of the class below to show as well as the logcat i get when it the application crashes

main code:

public class WorkoutChoice extends Activity
{
 private TextView mDateDisplay;
 private Button mPickDate;
 private int mYear,mMonth,mDay;
 private int cYear,cMonth,cDay;
 static final int DATE_DIALOG_ID = 0;
 Button   mButton;
 EditText cweight;
 EditText nweight;
 TextView t;
 String s,s2,cDate;
 int current,target;
 DataBaseHelper db = new DataBaseHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.choice);

    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    mPickDate = (Button) findViewById(R.id.pickDate);
    mPickDate.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
    final Calendar c = Calendar.getInstance();
    cYear = c.get(Calendar.YEAR);
    cMonth = c.get(Calendar.MONTH);
    cDay = c.get(Calendar.DAY_OF_MONTH);
    cDate = (""+cYear+""+cMonth+""+cDay);
    Date past = new Date(112, cMonth, cDay); // current Date
    Date today = new Date(112, 11, 18); // date Choosen by the user
    final int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays()+1;
     mDateDisplay.setText(cDate);
     //display the current date (this method is below)
     updateDisplay();

    mButton = (Button)findViewById(R.id.get);
    cweight   = (EditText)findViewById(R.id.cweight);
    nweight   = (EditText)findViewById(R.id.nweight);
    t = (TextView)findViewById(R.id.out);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                db.open();
               s = WorkoutChoice.this.cweight.getText().toString();
               current =  Integer.parseInt(s);
               s2 = WorkoutChoice.this.nweight.getText().toString();
               target =  Integer.parseInt(s2);
               db.deleteFirst();
               db.insertTitle("001", current, target);
               db.close();
            }

        });

}
public void convert(View view) 
{
    Intent i = new Intent();
    i.setClassName("com.b00348312.workout","com.b00348312.workout.convert");
    startActivity(i); 
}
 private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    } 
 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;
            }
}

Logcat:

05-26 21:37:25.928: E/AndroidRuntime(334): FATAL EXCEPTION: main
05-26 21:37:25.928: E/AndroidRuntime(334): java.lang.IllegalArgumentException: current should be >= start and <= end
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.updateDaySpinner(DatePicker.java:364)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.updateSpinners(DatePicker.java:350)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.init(DatePicker.java:346)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:124)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:83)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.b00348312.workout.WorkoutChoice.onCreateDialog(WorkoutChoice.java:123)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.onCreateDialog(Activity.java:2472)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.createDialog(Activity.java:881)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.showDialog(Activity.java:2547)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.showDialog(Activity.java:2514)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.b00348312.workout.WorkoutChoice$2.onClick(WorkoutChoice.java:56)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.view.View.performClick(View.java:2408)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.view.View$PerformClick.run(View.java:8816)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Handler.handleCallback(Handler.java:587)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-26 21:37:25.928: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:37:25.928: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:521)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-26 21:37:25.928: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Darren Murtagh
  • 591
  • 2
  • 6
  • 28

2 Answers2

1

You never initialize mYear, mMonth, or mDay before using them.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I do at the beginning of the code. private int mYear,mMonth,mDay; private int cYear,cMonth,cDay; – Darren Murtagh May 26 '12 at 21:54
  • 1
    @DarrenMurtagh: You *declared* them. You did not *initialize* them to valid values. They will each be `0`, and `0` is not a valid day of the month, for example. – CommonsWare May 26 '12 at 21:55
  • so what do i initialize them as? – Darren Murtagh May 26 '12 at 21:55
  • @DarrenMurtagh: This is covered in [the documentation for the constructor for `DatePickerDialog`](http://goo.gl/6yTtL). The values you pass into the `DatePickerDialog` constructor are the initial year, month, and day of month to be displayed. Right now, you are passing the uninitialized values of `mYear`, etc. However, I cannot tell you what to initialize them to, because **this is your app**, and I do not know what the business logic is supposed to be. Perhaps your starting date is today, or the date of Beethoven's birth, or the date of Ragnarök, or something else. That's your job. – CommonsWare May 26 '12 at 22:00
  • so i initialize them as a date before putting them on to the datepicker then? – Darren Murtagh May 26 '12 at 22:05
  • ok now that i have the picker working how would i get the picked date into the main body of the code. – Darren Murtagh May 26 '12 at 22:28
  • @DarrenMurtagh: You already are, in `onDateSet()`. Here is a sample application that uses `DatePickerDialog` and `TimePickerDialog`: https://github.com/commonsguy/cw-android/tree/master/Fancy/Chrono – CommonsWare May 26 '12 at 22:31
0

It's pretty simple, use the following,

dtpStart.init(now.getYear(), now.getMonth(), now.getDay(),new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) { dateStart = (Date) new Date(year, monthOfYear,dayOfMonth); } }
Where dtpStart is the ID of my Date Picker, and dateStart is a java.util.Date object which is declared globally so that it can be accessed inside any functions/handlers throughout my Activity

Hope it helps!

Ashwin Balani
  • 745
  • 7
  • 19