有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我在Android中试图从字符串中解析整数时会遇到NumberFormatException?

我正在寻找一个自定义ROM的工具更新功能。我使用这段代码http://www.安卓snippets.com/check-for-updates-once-a-day作为基础,很快就意识到我不能使用Dropbox来部署这段代码。所以我修改了代码,下载了一个版本。dropbox中的txt文件。该文件包含一个数字(最新版本代码,在本例中为11)。我需要读取该行并将字符串解析为整数,以便与当前版本代码进行比较,如果存在更新,则触发下载。 除了解析txt文件中的int之外,所有代码都可以工作

这是我的密码:

    private Thread checkUpdate = new Thread() {
public void run() {
    try {

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard,"BPversion.txt");
        StringBuilder text = new StringBuilder();
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);

        int curVersion = getPackageManager().getPackageInfo("com.JB15613.BPcolorTool", 0).versionCode;
        System.out.println(line);
        int newVersion = 0;
        System.out.println(line);

        try {
            newVersion = Integer.parseInt(line);
            System.out.println(line);
        } catch(NumberFormatException nfe) {
            System.out.println("Exception " + nfe);
        }

        if (newVersion > curVersion) {
            mHandler.post(showUpdate);
        }    
        }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC", "Caught exception");

    }
}

我有一个例外:

    Exception java.lang.NumberFormatException: Invalid int: "11"

当我应用断点并运行调试器时,它在parseInt行崩溃

11在我看来是个有效的整数

非常感谢您的帮助


共 (0) 个答案