Frage

Ich war in der Lage, dies zu beheben, aber ich frage mich, ob ich eine gute Erklärung dafür bekommen, warum diese gebrochen wurde.

habe ich eine Nutzenfunktion mein Datum Konvertierungen mit der Datenbank für eine Android-Anwendung zu behandeln. Dieser Code ist fehlgeschlagen das zweite Mal auf sie zugegriffen wurde:

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);
  } 
}

Ich verstehe, dass die verschiedenen Merkmale Simple nicht Thread-sicher sind, aber (soweit ich weiß) mein Programm wurde in einem einzigen Thread ausgeführt wird. Ich bin neu in Android und vielleicht bin ich falsch. Ich weiß, dass ich in der Lage war, dies zu beheben, indem die ParsePosition in die Methoden zu bewegen und Newing es. Auch wenn es sicher nicht Thread ist, ist es nicht so ziemlich nur eine Konstante? Warum sollte ein Nur-Lese-Konstante Pause ein Thread?

Danke!

EDIT: Hier ist die 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.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2753): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 2753):    at Util.isoToDate(Util.java:34)
War es hilfreich?

Lösung 2

Es stellt sich heraus, dass es die ParsePosition war, die die Probleme verursacht wurde. Instanziieren es gelöst jedes Mal das Problem.

Andere Tipps

In isoToDate, stellen Sie sicher, dass isodate nicht null ist. So etwas wie:

public static Date isoToDate(String isodate) {
   if (isodate == null) {
      // Raise exception, or return default date
   }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top