Wednesday, March 14, 2012

How to Crash a Galaxy S II via the Date Class

We just ran up against this one in a project that's set to launch any day now: a crash that only happened on Samsung Galaxy S II phones, only when set to the Sydney, Australia time zone, and only when using the DD/MM/YYYY date format.

The culprit was eventually tracked down to be the way we were storing time stamps in our text-based save data. This simplified example of what we were doing will crash the above configuration every time:

final Date source = new Date();
final String serialized = source.toString();
final Date dest = new Date( serialized );


The solution was pretty straightforward once we identified the problem. We now store the milliseconds value from the Date object rather than the formatted display text:

final Date source = new Date();
final String serialized = String.valueOf( source.getTime() );
final Date dest = new Date( Long.parseLong(serialized) );


It's a simpler approach and therefore probably what we should have been using since the start, but that's with the benefit of hindsight. The original approach is totally valid according to the Android documentation and survived months of mixed casual and focused testing. It's a pretty good example of one of the weird problems an application can run into out in the wild that are almost impossible to anticipate.

We were extremely lucky to have a someone testing the application in just the right place and with just the right hardware. He snipped a copy of the system log containing the crash from his phone using alogcat. That led us down the path of being able to locally reproduce the problem and ultimately fix it.

No comments:

Post a Comment