2016-03-11 1 views
1

J'essaie d'obtenir une bibliothèque Java externe pour travailler avec Android, mais ses erreurs de lancement tout en travaillant avec des dates.Long.parseLong ("+ 2013") throws NumberFormatException

Je l'ai réduit à cette ligne:

Long year = Long.parseLong("+2013"); 

qui jette un

Caused by: java.lang.NumberFormatException: Invalid long: "+2013" 

Cependant, le code est valide et travailler dans une application Java. Pourquoi le Long.parseLong() fonctionne-t-il différemment dans une application Android?

Android's documentation indique que '+' signes Ascii sont reconnus:

public static long parseLong (String string) 

parse la chaîne spécifiée comme une valeur décimale signée à long. Les caractères ASCII - ('-') et + ('+') sont reconnus comme les signes moins et plus.

+0

-t-il dire la même chose pour ** - 2013 ** ainsi? – camelCaseCoder

+0

Avez-vous essayé avec 'long year = Long.parseLong (" + 2013 ");'? – hoomi

+0

@hoomi J'ai testé avec la primitive 'long' et l'objet' Long' avec les deux ** + 2013 ** et ** - 2013 **, il n'y a pas eu d'exception. Bizarre. – camelCaseCoder

Répondre

0

Merci à tous! Géré pour trouver le problème.

Il semble que l'API 17: Android 4.2 (Jelly Bean) cause des problèmes. L'API ne gère pas correctement le signe '+' dans le parseLong().

L'a résolu en modifiant le SDK de compilation en API 21 et versions ultérieures.

API 20 parseLong()

/** 
* Parses the specified string as a signed long value using the specified 
* radix. The ASCII character \u002d ('-') is recognized as the minus sign. 
* 
* @param string 
*   the string representation of a long value. 
* @param radix 
*   the radix to use when parsing. 
* @return the primitive long value represented by {@code string} using 
*   {@code radix}. 
* @throws NumberFormatException 
*    if {@code string} cannot be parsed as a long value, or 
*    {@code radix < Character.MIN_RADIX || 
*    radix > Character.MAX_RADIX}. 
*/ 
public static long parseLong(String string, int radix) throws NumberFormatException { 
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 
     throw new NumberFormatException("Invalid radix: " + radix); 
    } 
    if (string == null) { 
     throw invalidLong(string); 
    } 
    int length = string.length(), i = 0; 
    if (length == 0) { 
     throw invalidLong(string); 
    } 
    boolean negative = string.charAt(i) == '-'; 
    if (negative && ++i == length) { 
     throw invalidLong(string); 
    } 

    return parse(string, i, radix, negative); 
} 

API 21 parseLong()

/** 
* Parses the specified string as a signed long value using the specified 
* radix. The ASCII characters \u002d ('-') and \u002b ('+') are recognized 
* as the minus and plus signs. 
* 
* @param string 
*   the string representation of a long value. 
* @param radix 
*   the radix to use when parsing. 
* @return the primitive long value represented by {@code string} using 
*   {@code radix}. 
* @throws NumberFormatException 
*    if {@code string} cannot be parsed as a long value, or 
*    {@code radix < Character.MIN_RADIX || 
*    radix > Character.MAX_RADIX}. 
*/ 
public static long parseLong(String string, int radix) throws NumberFormatException { 
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 
     throw new NumberFormatException("Invalid radix: " + radix); 
    } 
    if (string == null || string.isEmpty()) { 
     throw invalidLong(string); 
    } 
    char firstChar = string.charAt(0); 
    int firstDigitIndex = (firstChar == '-' || firstChar == '+') ? 1 : 0; 
    if (firstDigitIndex == string.length()) { 
     throw invalidLong(string); 
    } 

    return parse(string, firstDigitIndex, radix, firstChar == '-'); 
} 
+0

C'est bizarre. Malheureusement, je n'ai pas de périphérique 4.2 pour tester cela. De toute façon, content que votre problème soit résolu! – camelCaseCoder