Android上的Integer.parseInt和NumberFormatException [英] Integer.parseInt and NumberFormatException on Android

查看:378
本文介绍了Android上的Integer.parseInt和NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用中运行了以下代码

I ran the following code in my Android app

Integer.parseInt("+91");

Android 5.0 (Lollipop), 没有 抛出任何异常 +91 是一个整数。但是在 Android 4.4.x (KitKat)和更低版本中抛出:

In Android 5.0 (Lollipop), it did not throw any exception as +91 is an integer. But in Android 4.4.x (KitKat) and lower versions it throws:


NumberFormatException:无效的Int:+ 91

NumberFormatException : Invalid Int : "+91"

Android版本如何造成这种差异?

How is the version of Android causing this difference?

推荐答案

支持显式 此提交中添加了+

Support for explicit + was added in this commit:

Support explicit + in Byte, Short, Integer, Long.

Bug: 5239391
Change-Id: I2b25228815d70d570d537db0ed9b5b759f25b5a3

已包含从 android-5.0.0_r1 开始。如果您已经获取了Git存储库,则可以使用以下命令进行验证:

which has been included starting with android-5.0.0_r1. If you have fetched the Git repository, you can verify with:

git tag --contains 6b40837ee3a023bba698c38fd6d6e46ae0065a55

给你

android-5.0.0_r1
android-5.0.0_r2
android-5.0.0_r3
...

尽管文档可以深入了解为什么进行了更改(为了实现Java 7的行为,正如其他答案所指出的那样),分析源代码的历史记录可以提供最多的信息。 行为发生变化时的准确答案,因为文档不一定与实现相匹配。

Even though documentation can give insights into why the change was made (to achieve Java 7 behavior as other answers point out), analyzing the history of the source code gives the most accurate answer to when the behavior changed, since documentation does not necessarily match implementation.

这篇关于Android上的Integer.parseInt和NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆