简体   繁体   English

Android DatePickerDialog-失败,出现IllegalArgumentException

[英]Android DatePickerDialog - fails with IllegalArgumentException

Something odd that's happening on my Android emulator (Code and emulator run on API Level 10 - being Android 2.3.3): The current date on my emu is 22.08.2011 - if I initialize the DatePickerDialog with those settings (using the values from new Date() - which initializes the Date with the current date/time) then it fails, with the following exception: 我的Android模拟器上发生了一些奇怪的事情(代码和模拟器在API级别10上运行-是Android 2.3.3):e的当前日期是22.08.2011-如果我使用这些设置初始化DatePickerDialog(使用来自new的值Date()-使用当前日期/时间初始化Date),然后失败,但以下情况除外:

08-22 02:14:23.731: ERROR/AndroidRuntime(3038): FATAL EXCEPTION: main
08-22 02:14:23.731: ERROR/AndroidRuntime(3038): java.lang.IllegalArgumentException: current should be >= start and <= end
08-22 02:14:23.731: ERROR/AndroidRuntime(3038):     at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
08-22 02:14:23.731: ERROR/AndroidRuntime(3038):     at android.widget.DatePicker.updateSpinners(DatePicker.java:357)
08-22 02:14:23.731: ERROR/AndroidRuntime(3038):     at android.widget.DatePicker.init(DatePicker.java:352)
08-22 02:14:23.731: ERROR/AndroidRuntime(3038):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:127)
08-22 02:14:23.731: ERROR/AndroidRuntime(3038):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:86)

So I checked out the source Code for DatePicker (For API lvl 2.3.3) and I checked the DatePicker code (because from the strack trace I knew it fails in the updateSpinners method) and at that line 357 (fourths line in the above stack trace) it calls mYearPicker.setCurrent(mYear); 因此,我签出了DatePicker源代码 (对于API lvl 2.3.3),并检查了DatePicker代码(因为从strack跟踪中得知在updateSpinners方法中它失败了)并在第357行(上面堆栈的第四行)跟踪)它调用mYearPicker.setCurrent(mYear); which made me think something must be wrong with my year. 这让我觉得我的一年一定有问题。 I logged the year I am passing - it's 2011 as expected. 我记录了我过去的那一年-如预期的那样是2011年。 So I manually passed the year 2010 instead which worked - the DatePickerDialog showed up - initialized with the year 2010 (too bad, that I really need the current year - 2011 ;) ...). 因此,我手动通过了有效的2010年-显示了DatePickerDialog-初始化为2010年(太糟糕了,我确实需要当前的2011年;)...)。

I am a bit at a loss as to what the reason is for this... The date on my emulator - as mentioned above - is correctly set to 22.08.2011 - my dev PC is set to the same date... 我有点茫然,这是由什么原因造成的...如上所述,我模拟器上的日期正确设置为2011年8月22日-开发人员PC的日期设置为相同的日期...

Any clues/ideas as to where I am going wrong? 关于我要去哪里的任何线索/想法? To make things easier here is the code snippet where I am initializing the DatePickerDialog: 为了使事情变得容易,这里是我初始化DatePickerDialog的代码片段:

// Initializes a Date object with the current date and time for clarification:
// it is actually declared as a private member variable of my class - 
// just included it here so you guys know of which type dateTime ist

 Date dateTime = new Date();
 Button dateButton = new Button(context);
 dateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
// dateSetListener is declared as well - but its code has no influence on the failure of 
// the DatePickerDialog - to ensure this I commented all code in the dateSetListener's 
// onDateSet method 

                Dialog dateDialog = new DatePickerDialog(v.getContext(), dateSetListener, dateTime.getYear(),
                        dateTime.getMonth(), dateTime.getDay());
                dateDialog.show();
            }
        });

Thanks in advance & best regards, Ready4Android 提前致以最诚挚的感谢,Ready4Android

Okay after a bit more fiddeling I resorted to something I should have done WAY before: I logged the value of 好吧,经过一番沉思之后,我求助于我之前应该做的事情:我记录了

//As in my example dateTime was initialized with new Date()
dateTime.getYear()

And the value the log showed was 111 ! 日志显示的值为111 I know this method is deprecated and apprently for good reasons... My mistake was to not verify that the value 2011 is returned - instead 111 is returned - as the documentation of getYear() says, it returns the years since 1900 - if we add 111 to 1900 we end up with 2011 ... I was tempted for a second to simply add 1900 to the return value but sanity got the better of me and I decided to use the java class Calendar instead. 我知道此方法已被弃用,原因有误……我的错误是不验证是否返回了值2011-返回了111-正如getYear()的文档所述,它返回自1900年以来的年份-如果我们如果将1900加上111,我们最终会得到2011年。。。我想了一秒钟,只是简单地将1900添加到返回值中,但是理智使我变得更好,我决定改用Java类Calendar

The reason why I was mislead at first is, that formatting my date using SimpleDateFormat resulted in the correct year - I did not consider the interior working of SimpleDateFormat, otherwise I might have had a clue that I am on the wrong track with my Date object. 一开始我被误导的原因是,使用SimpleDateFormat格式化日期会导致正确的年份-我没有考虑过SimpleDateFormat的内部工作方式,否则我可能已经知道我的Date对象走错了路。

To sum it up: There is no Problem with the DatePickerDialog - the problem was with the Date values I passed to it. 总结一下:DatePickerDialog没有问题-问题出在我传递给它的Date值上。 However it would have been nice, if the error message would have mentioned the exact values instead of current should be >= ... 但是,如果错误消息中提到的是确切值而不是当前值,则应该是> = ...

I think it will not compile, make dateTime field final . 我认为它不会编译,使dateTime字段为final Also, have you declared dateSetListener ? 另外,您是否声明了dateSetListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM