解析String到Int的android java.lang.NumberFormatException

时间:2014-04-05 07:58:16

标签: android string parsing integer

我想做的是...我想从我的文件夹中列出所有png文件名为.." TEST" ..我给的名字是1.png,2.png ......就像那样..并且取决于最后没有(即3)我想添加下一个int和(值将4)将该名称提供给下一个下载文件...(表示4.png)。我在做什么错了?

我做到了这一点......

Integer x;
String downloadDirectory = "/MY_TESTING"; 

           File newDownloadDirectory = new File(Environment.getExternalStorageDirectory() + downloadDirectory);

        File listFile[] = newDownloadDirectory.listFiles();
        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {

                if (listFile[i].isDirectory()) {
                    fileList.add(listFile[i]);
                    getfile(listFile[i]);

                } else {
                    if (listFile[i].getName().endsWith(".PNG")
                            || listFile[i].getName().endsWith(".jpg")
                            || listFile[i].getName().endsWith(".jpeg")
                            || listFile[i].getName().endsWith(".gif"))

                    {
                        fileList.add(listFile[i]);
                    }
                }

            }





        // filesize = fileList.size();
        if (fileList.size() == 0) {
            GlobalVariable.setCounter(1);
            Log.i("hhhhelo ", GlobalVariable.getCounter() + "");
            GlobalVariable.setStrPath(GlobalVariable.getCounter() + ".PNG");
        } else {
            int a[] = new int[fileList.size()];
            Integer maximum = 0;

            for (int i = 0; i < fileList.size(); i++) {
                String s = fileList.get(i).toString();
                String str = s.substring(0, s.indexOf(".PNG"));
                System.out.println("TESTTTTTT.."+str);
                System.out.println("str in PlaybackMP3: " + str);

                 x = Integer.valueOf(str);
                System.out.println("cntr in PlaybackMP3: " + x);
                a[i] = x;
                if (a[i] > maximum) {
                    maximum = a[i];
                }
            }

但我在x = Integer.valueOf(str);

上收到错误

Logcat说:

04-05 13:21:31.631: E/AndroidRuntime(893): FATAL EXCEPTION: main
04-05 13:21:31.631: E/AndroidRuntime(893): java.lang.NumberFormatException: Invalid int: "/storage/sdcard0/MY_TESTING/1"
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.Integer.invalidInt(Integer.java:138)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.Integer.parse(Integer.java:375)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.Integer.parseInt(Integer.java:366)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.Integer.parseInt(Integer.java:332)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.Integer.valueOf(Integer.java:490)
04-05 13:21:31.631: E/AndroidRuntime(893):  at com.leeway.hdwallpaper.Test_My_Paging_Activity.save_Image(Test_My_Paging_Activity.java:137)
04-05 13:21:31.631: E/AndroidRuntime(893):  at com.leeway.hdwallpaper.Test_My_Paging_Activity$3.onLongClick(Test_My_Paging_Activity.java:86)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.view.View.performLongClick(View.java:4207)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.view.View$CheckForLongPress.run(View.java:17174)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.os.Handler.handleCallback(Handler.java:643)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.os.Looper.loop(Looper.java:137)
04-05 13:21:31.631: E/AndroidRuntime(893):  at android.app.ActivityThread.main(ActivityThread.java:4803)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.reflect.Method.invokeNative(Native Method)
04-05 13:21:31.631: E/AndroidRuntime(893):  at java.lang.reflect.Method.invoke(Method.java:511)
04-05 13:21:31.631: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-05 13:21:31.631: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-05 13:21:31.631: E/AndroidRuntime(893):  at dalvik.system.NativeStart.main(Native Method)
04-05 13:21:32.942: E/Trace(956): error opening trace file: No such file or directory (2)
04-05 13:21:33.964: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:21:33.964: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:21:44.345: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:21:44.345: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:21:45.526: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:21:45.526: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:22:16.139: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:22:16.139: E/Parcel(304): Reading a NULL string not supported here.
04-05 13:22:41.415: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:41.466: E/Trace(1139): error opening trace file: No such file or directory (2)
04-05 13:22:41.506: E/MediaPlayer_Java(1219): Uri is  <URL suppressed>
04-05 13:22:41.626: E/Trace(1165): error opening trace file: No such file or directory (2)
04-05 13:22:41.686: E/Trace(1180): error opening trace file: No such file or directory (2)
04-05 13:22:41.746: E/SQLiteLog(651): (1) no such column: _data
04-05 13:22:44.188: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.188: E/NotificationService(651): Ignoring notification with icon==0: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 kind=[null])
04-05 13:22:44.198: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.208: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.218: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.279: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.319: E/ALSADevice(288): use case is HiFi
04-05 13:22:44.349: E/ALSADevice(288): use case is HiFi
04-05 13:22:44.379: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.399: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.409: E/ACDB-LOADER(288): Error: ACDB AudProc vol returned = -8
04-05 13:22:44.509: E/bsthal(651): LightSensor: (type=3, code=0)
04-05 13:22:44.869: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.919: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:44.939: E/bsthal(651): virtual int ProximitySensor::enable(int32_t, int):write 1 !
04-05 13:22:44.939: E/bsthal(651): virtual int ProximitySensor::enable(int32_t, int):/sys/class/input/input6//enable/enable open !
04-05 13:22:45.400: E/[WP1]The number not match!, run original flow =(1219): +911409964001
04-05 13:22:45.450: E/[WP1]The number not match!, run original flow =(1219): +911409964001

我也试过了。

cntr = Integer.parseInt(str);

4 个答案:

答案 0 :(得分:0)

为什么使用".PNG"代替".png" ???

因为 这一行

String str = s.substring(0, s.indexOf(".PNG"));

返回null, 我认为它是由错误的文件命名引起的,例如:1或1.png 正确的是1.PNG到子串不会为空,

答案 1 :(得分:0)

您收到该错误的原因是因为您尝试转换为String的{​​{1}}的值为Integer。您无法将其转换为数字,这就是您收到错误的原因。

如果您知道所有文件都位于"storage/sdcard0/MY_TESTING/1",那么您只需使用"storage/sdcard0/MY_TESTING/",然后拨打String.replace("storage/sdcard0/MY_TESTING/", "");;

Integer.parse()

答案 2 :(得分:0)

正如tambykojak所述,您可以在日志中看到使用toString()方法获取整个路径,而不仅仅是文件名。如果你想要这个名字,你应该使用它:

FilenameUtils.getBaseName(fileList.get(i).getName())

这将返回没有路径AND而没有扩展名的名称。 (所以你可以删除substring()方法)

不要手动从路径中提取名称,因为它不会灵活且难以维护。

答案 3 :(得分:0)

这个工作

 String path = Environment.getExternalStorageDirectory().toString()
                    + "/MY_TESTING";
            Log.d("Files", "Path: " + path);
            File f = new File(path);
            File file[] = f.listFiles();
            Log.d("Files", "Size: " + file.length);
            for (int i = 0; i < file.length; i++) {
                Log.d("Files", "FileName:" + file[i].getName());
            }