[mono-android] Java exception error with DatePicker - any advice?
Jonathan Pryor
jonp at xamarin.com
Tue Jan 3 17:35:23 EST 2012
On Dec 29, 2011, at 12:18 PM, Paul F. Johnson wrote:
> I've implemented a DateTimeListener event which compiles fine, but dies when the emulator comes to run the code with the following error: Unhandled Exception
...
> with the call in the code looking like this
>
> DateTime today = DateTime.Now;
>
> DatePicker dater = FindViewById<DatePicker>(Resource.Id.dateDisplay);
> dater.Init(today.Year, today.Month, today.Day, new DateChangedListener((DatePicker, year, month, day) =>
> {
> DateTime d = new DateTime(year, month, day);
> common.date = d;
> }));
The problem is a semantic mismatch between Android dates and .NET dates: Android months are 0-based, while .NET dates are 1-based.
For example, if it happens to be December, DateTime.Now.Month will be 12, which is out of range for Android. Result: Java.Lang.IllegalArgumentException in the Init() call.
If instead you run this in January, the Init() call will succeed, but when the callback is invoked the `month` parameter will be 0, and .NET will throw an ArgumentOutOfRangeException.
The differing month systems need to be translated. :-/
- Jon
More information about the Monodroid
mailing list