Search code examples
javaandroidandroid-datepicker

datePicker.setMinDate(System.currentTimeMillis()) gives error


When i try to set the minimum date for the DatePicker view it gives me this error:

Call requires API level 11 (current min is 8): android.widget.DatePicker#setMinDate

My java is:

datePicker = (DatePicker) findViewById(R.id.date_picker_id);
datePicker.setMinDate(System.currentTimeMillis() - 259200000);

My XML:

<DatePicker
    android:id="@+id/date_picker_id"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_below="@id/header_id" 

    />

How do i solve this error?


Solution

  • The DataPicker.setMinDate() method was added only in android API level 11 (Android 2.3.4 I guess)... But in your AndroidManifest.xml you have set the minSDKVersion=8 (Android 2.2)...

    So obviously when some device running version 2.2 executes your code, it won't know about the setMinDate() method... Hence the error...

    To Fix:

    1. Set the minSDKVersion to 11. But people with android OS less than 2.3.4 will not be able to install your app.
    2. Remove the method and use only those methods which are available from API level 8.