2

IntelliJ IDEA 11.1.2を使用してAndroidアプリケーションを開発していますが、入力フォームにdatepickerが必要です。しかし、それを挿入すると、IDEAはプレビューウィンドウに「Missingclass DatePicker」警告を表示し、クラッシュする電話でアプリケーションを起動します。datepicker要素を削除すると、アプリケーションは正しく機能します。

ターゲットプラットフォームとしてandroid2.2を使用し、javaSDKとしてjava1.6を使用しています。

これが私のフォームのソースコードです:

<?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>

どうすればこの問題を解決できますか?

4

1 に答える 1

2

更新:この問題のバグレポートを提出しました(Android <= 2.3)。

だから私はあなたが持っているケースを再現することができました。実際、 IDEAは、何らかの問題があり、アプリがクラッシュしていることを示しています。Eclipseを使用しても効果はありません(チェック済み)-そこにあるグラフィックレイアウトビューも、オブジェクトを作成できないことを示しDatePickerます。

根本的な原因

あなたの場合の問題は、デバイスに現在の年が含まれandroid:startYearandroid:endYearいないことです(2011年から2011年の範囲には2012年から現在の年は含まれていません)。

これはおそらくDatePicker実装のバグ(API 11より前)であり、ウィジェットを現在の日付で初期化し、現在の年が含まれる範囲を定義しようandroid:startYearとするかどうかは関係ありません。android:endYear

解決

android:minDateAPIレベル9を使用しているため、またはandroid:maxDateプロパティはありません(API 11で導入されました)。したがって、日付の選択を任意の年の範囲に制限するために(上記のバグの回避策)、独自のを実装できますOnDateChangedListener

コメント付きのサンプルコード:

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);
                        }
                    }

                });
    }
}
于 2012-07-18T14:46:32.620 に答える