当前位置: 代码迷 >> Android >> 为何会出现numberFormatException
  详细解决方案

为何会出现numberFormatException

热度:91   发布时间:2016-04-28 00:19:34.0
为什么会出现numberFormatException
我在给listView的图片准备资源的时候,写了如下代码:
for(int i = 0; i < goodsLength; i++) {
String sb = "cla_" + String.valueOf(i + 1);
goodsClaPic[i] = resourses.getIdentifier(
sb, "drawable", packageName);
}
结果运行到这,总是跳到下面的函数去:
private static NumberFormatException invalidInt(String s) {
        throw new NumberFormatException("Invalid int: \"" + s + "\"");
    }
这是为什么呢?
------解决思路----------------------
引用:
引用
resourses.getIdentifier这个方法是干嘛的

从drawable调用资源的方法!

你去看源码……

    public int getIdentifier(String name, String defType, String defPackage) {
        if (name == null) {
            throw new NullPointerException("name is null");
        }
        try {
            return Integer.parseInt(name);
        } catch (Exception e) {
            // Ignore
        }
        return mAssets.getResourceIdentifier(name, defType, defPackage);
    }

第一个参数会调用 Integer.parseInt(name);
所以就抛出异常了NumberFormatException
------解决思路----------------------



引用:
Quote: 引用:

引用:
Quote: 引用:

Quote: 引用:

引用
resourses.getIdentifier这个方法是干嘛的

从drawable调用资源的方法!

你去看源码……

    public int getIdentifier(String name, String defType, String defPackage) {
        if (name == null) {
            throw new NullPointerException("name is null");
        }
        try {
            return Integer.parseInt(name);
        } catch (Exception e) {
            // Ignore
        }
        return mAssets.getResourceIdentifier(name, defType, defPackage);
    }

第一个参数会调用 Integer.parseInt(name);
所以就抛出异常了NumberFormatException

name中含有非数字吧


都别扯可好,可用过?
你看到源码上把那个异常打印出来了?抛出来了?没看到Ignore啊。
这个方法的作用本身就是通过文件名查到资源id的。还说得像真的一样。

你把异常信息贴出来。就你那样的描述,不清楚。
  相关解决方案