Reputation: 23
I am developing an android application using IntelliJ IDEA 11.1.2 and I need datepicker on my input form. However, when I insert it, IDEA gives "Missing class DatePicker" warning in the preview window and I launch application on the phone it crashes. If I remove datepicker element, application works properly.
I am using android 2.2 as a target platform and java 1.6 as java SDK.
Here's source code of my form:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/l_main"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="Some text: "
android:layout_width="fill_parent"
android:textSize="@dimen/fnt_size"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/source"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Some text: "
android:layout_width="fill_parent"
android:textSize="@dimen/fnt_size"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Some text: "
android:layout_width="fill_parent"
android:textSize="@dimen/fnt_size"
android:layout_height="wrap_content"
/>
<DatePicker
android:id="@+id/date"
android:endYear="2011"
android:startYear="2011"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/submitBtn"
android:text="Search"
android:layout_width="@dimen/btn_size"
android:textSize="@dimen/fnt_size"
android:layout_height="wrap_content" />
</LinearLayout>
How can I solve this problem?
Upvotes: 2
Views: 1123
Reputation: 4408
UPDATE: I have filed bug report for this issue (Android <= 2.3).
So I was able to reproduce case that you have. Indeed IDEA signals that's there's some issue and app is crashing. Using Eclipse will not help (checked it) - Graphical Layout view in there will also signals that it cannot create DatePicker
object.
It occurs that the problem in your case is that android:startYear
and android:endYear
do not include current year on the device (range 2011-2011 does not include 2012 - current year).
This is probably a bug in DatePicker
implementation (prior to API 11) which tries to init the widget with current date regardless whether or not android:startYear
and android:endYear
define range that have current year within it.
Because you are using API level 9, there are no android:minDate
or android:maxDate
properties (introduced in API 11). So to restrict date picking to any range of years (workaround for the above mentioned bug), you can implement your own OnDateChangedListener
.
Example code with comments:
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datePicker = (DatePicker) findViewById(R.id.date);
final int MIN_YEAR = 2011;
final int MAX_YEAR = 2011;
datePicker.init(MIN_YEAR /* initial year */,
01 /* initial month of year */,
01 /* initial day of month */,
new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker,
int year,
int monthOfYear,
int dayOfMonth) {
// Override changing year to anything different from outside the range <MIN_YEAR, MAX_YEAR>
if (year < MIN_YEAR) {
datePicker.updateDate(MIN_YEAR, monthOfYear, dayOfMonth);
} else if (year > MAX_YEAR) {
datePicker.updateDate(MAX_YEAR, monthOfYear, dayOfMonth);
}
}
});
}
}
Upvotes: 2