开发者

SimpleDateFormat Failing in Android

开发者 https://www.devze.com 2023-01-19 21:30 出处:网络
I was able to fix this, but I am wondering if I can get a good explanation for why this was broken. I created a utility function to handle my date conversions with the database for an android applica

I was able to fix this, but I am wondering if I can get a good explanation for why this was broken.

I created a utility function to handle my date conversions with the database for an android application. This code failed the second time it was accessed:

public class Util {

  private static final ParsePosition pos = new ParsePosition(0);

public static String isoDateFormat(Date d) {
      SimpleDateFormat databaseformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return databaseformat.format(d);
  }

  public static Date isoToDate(String isodate) {
      SimpleDateFormat databaseformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return databaseformat.parse(isodate, pos);
  } 
}

I understand that the various SimpleDateFormat features are not thread safe, but (as far as I know) my program was running inside a single thread. I am new to android and maybe I'm mistaken. I do know that I was able to fix this by moving the ParsePosition into the methods and newing it. Even if it's not thread safe, isn't it pretty much just a constant? Why would a read only constant break a thread?

Thanks!

EDIT: Here is the stacktrace:

E/AndroidRuntime( 2753): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobile/com.example.mobile.ExampleSelector}: java.lang.NullPointerException
E/AndroidRuntime( 2753):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 2753):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 2753):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2753):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2753):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2753):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2753):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2753):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2753):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2753):    at dalvik.sys开发者_如何转开发tem.NativeStart.main(Native Method)
E/AndroidRuntime( 2753): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 2753):    at Util.isoToDate(Util.java:34)


In isoToDate, make sure that isodate is not null. Something like:

public static Date isoToDate(String isodate) {
   if (isodate == null) {
      // Raise exception, or return default date
   }
}


It turns out it was the ParsePosition that was causing the problems. Instantiating it each time solved the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号