gpt4 book ai didi

java - 缺少类(class)日期选择器

转载 作者:行者123 更新时间:2023-11-30 09:35:04 25 4
gpt4 key购买 nike

我正在使用 IntelliJ IDEA 11.1.2 开发一个 Android 应用程序,我的输入表单需要日期选择器。但是,当我插入它时,IDEA 在预览窗口中发出“Missing class DatePicker”警告,我在手机上启动应用程序时崩溃了。如果我删除日期选择器元素,应用程序将正常运行。

我使用 android 2.2 作为目标平台,使用 java 1.6 作为 java SDK。

这是我的表单的源代码:

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

我该如何解决这个问题?

最佳答案

更新:我有 filed bug report对于此问题(Android <= 2.3)。

所以我能够重现您的案例。事实上,IDEA 发出信号表明存在一些问题并且应用程序正在崩溃。使用 Eclipse 无济于事(已检查)- Graphical Layout 那里的 View 也将表明它无法创建 DatePicker 对象。

根本原因

您遇到的问题是 android:startYearandroid:endYear 不包括设备上的当前年份(范围 2011-2011 不包括2012 年 - 当前年份)。

这可能是 DatePicker 实现(API 11 之前)中的错误 tries to init the widget with current date无论 android:startYearandroid:endYear 是否定义包含当前年份的范围。

解决方案

因为您使用的是 API 级别 9,所以没有 android:minDateandroid: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);
}
}

});
}
}

关于java - 缺少类(class)日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11542652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com