Спасибо всем! Удалось выяснить проблему.
Кажется, что API 17: Android 4.2 (Jelly Bean) вызывает проблемы. API не обрабатывает знак «+» в parseLong() правильно.
Решил, что он сменил SDK на API 21 и более поздних версий.
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 == '-');
}
ли сказать то же самое для ** - 2013 ** а? – camelCaseCoder
Вы пробовали с 'long year = Long.parseLong (" + 2013 ");'? – hoomi
@hoomi Я тестировал как примитивные 'long', так и объекты' Long' с ** ** + 2013 ** и ** - 2013 **, это не создавало исключения. Weird. – camelCaseCoder