java-使用File.listfiles()获取NullPointer异常

java-使用File.listfiles()获取NullPointer异常,第1张

概述所以我的AndroidGallery中有3个文件夹.我试图将这些文件夹的文件读入File数组,然后从那里将图像源转换为实际的ImageViews.我的问题是,引用文件数组时出现NullPointerException.这是一些代码://Getimagefilespaths.privateFiletopsFolder=newFile(Environment.getExte

所以我的Android gallery中有3个文件夹.我试图将这些文件夹的文件读入file数组,然后从那里将图像源转换为实际的ImageVIEws.

我的问题是,引用文件数组时出现NullPointerException.这是一些代码:

// Get image files paths.private file topsFolder = new file(Environment.getExternalStorageDirectory().getPath() + "/DressIt tops/");private file bottomsFolder = new file(Environment.getExternalStorageDirectory()        .getPath() + "/DressIt Bottoms/");private file shoesFolder = new file(Environment.getExternalStorageDirectory()        .getPath() + "/DressIt Shoes/");private file[] alltops = topsFolder.Listfiles();private file[] allBottoms = bottomsFolder.Listfiles();private file[] allShoes = shoesFolder.Listfiles();

使用这些图像文件数组…

ImageAdapter topAdapter = new ImageAdapter(createList(alltops));    topList.setAdapter(topAdapter);    ImageAdapter mIDAdapter = new ImageAdapter(createList(allBottoms));    mIDList.setAdapter(mIDAdapter);    ImageAdapter botAdapter = new ImageAdapter(createList(allShoes));    botList.setAdapter(botAdapter);

我的CreateList方法.错误是当我引用List.length时.

private List<ImageClass> createList(file[] List) {    List<ImageClass> result = new ArrayList<ImageClass>();    ImageClass img;    Bitmap bmp;    if (List.length > 0) {        for (int i = 0; i < List.length; i++) {            bmp = BitmapFactory.decodefile(List[i].getabsolutePath());            img = new ImageClass(bmp);            result.add(img);        }    } else        result.add(new ImageClass(this.getResources().getIDentifIEr(                "no_imgs_yet", "drawable", this.getPackagename())));    return result;}

就像我说的那样,错误是当我使用List.length引用一个file数组时,例如我在第一个代码段中创建的alltops.

这是LogCat错误消息:

05-05 19:43:49.409: D/dalvikvm(537): Not late-enabling CheckJNI (already on)05-05 19:43:51.069: D/dalvikvm(537): GC_FOR_ALLOC freed 70K, 3% free 10185K/10439K, paused 63ms05-05 19:43:51.079: I/dalvikvm-heap(537): Grow heap (frag case) to 10.566MB for 562516-byte allocation05-05 19:43:51.139: D/dalvikvm(537): GC_CONCURRENT freed 1K, 3% free 10733K/11015K, paused 6ms+7ms05-05 19:43:51.278: D/com.example.dressit(537): /mnt/sdcard/DressIt tops05-05 19:43:51.289: D/AndroIDRuntime(537): Shutting down VM05-05 19:43:51.289: W/dalvikvm(537): threadID=1: thread exiting with uncaught exception (group=0x409961f8)05-05 19:43:51.319: E/AndroIDRuntime(537): FATAL EXCEPTION: main05-05 19:43:51.319: E/AndroIDRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dressit/com.example.dressit.MainActivity}: java.lang.NullPointerException05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1955)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread.access0(ActivityThread.java:122)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.os.Handler.dispatchMessage(Handler.java:99)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.os.Looper.loop(Looper.java:137)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread.main(ActivityThread.java:4340)05-05 19:43:51.319: E/AndroIDRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)05-05 19:43:51.319: E/AndroIDRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)05-05 19:43:51.319: E/AndroIDRuntime(537):  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)05-05 19:43:51.319: E/AndroIDRuntime(537):  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)05-05 19:43:51.319: E/AndroIDRuntime(537):  at dalvik.system.NativeStart.main(Native Method)05-05 19:43:51.319: E/AndroIDRuntime(537): Caused by: java.lang.NullPointerException05-05 19:43:51.319: E/AndroIDRuntime(537):  at com.example.dressit.MainActivity.createList(MainActivity.java:125)05-05 19:43:51.319: E/AndroIDRuntime(537):  at com.example.dressit.MainActivity.onCreate(MainActivity.java:103)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.Activity.performCreate(Activity.java:4465)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)05-05 19:43:51.319: E/AndroIDRuntime(537):  at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1919)05-05 19:43:51.319: E/AndroIDRuntime(537):  ... 11 more05-05 19:43:51.509: D/dalvikvm(537): GC_CONCURRENT freed 420K, 5% free 10747K/11271K, paused 6ms+5ms05-05 19:44:25.629: I/Process(537): Sending signal. PID: 537 SIG: 9

我也有我的同意:

<uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" />

很抱歉投入了这么多代码,但是我觉得有人在这里找出问题是必要和充分的.

我现在已经花了几个小时,以为我会问社区!非常感谢大家.

编辑:感谢所有的反馈!这是我的代码,用于保存拍摄的图像.这应该显示所有已保存图像的路径.另外,我的LogCat说文件路径为/ mnt / sdcard / DressIt tops.

/** Create a file for saving an image or vIDeo */private static file getoutputMediafile(int type) {    // To be safe, you should check that the SDCard is mounted    // using Environment.getExternalStorageState() before doing this.    String dirname;    if (imageType == 1) {        dirname = "DressIt tops";    } else if (imageType == 2) {        dirname = "DressIt Bottoms";    } else        dirname = "DressIt Shoes";    file mediaStorageDir = new file(            Environment                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),            dirname);    // This location works best if you want the created images to be shared    // between applications and persist after your app has been uninstalled.    // Create the storage directory if it does not exist    if (!mediaStorageDir.exists()) {        if (!mediaStorageDir.mkdirs()) {            Log.d(dirname, "Failed to create directory");            return null;        }    }    // Create a media file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")            .format(new Date());    file mediafile;    if (type == MEDIA_TYPE_IMAGE) {        mediafile = new file(mediaStorageDir.getPath() + file.separator                + "img_" + timeStamp + ".jpg");    } else if (type == MEDIA_TYPE_VIDEO) {        mediafile = new file(mediaStorageDir.getPath() + file.separator                + "VID_" + timeStamp + ".mp4");    } else {        return null;    }    return mediafile;}

解决方法:

根据File.listFiles()的文档,如果file不是目录,它将返回null.

关于为什么会这样,有三种(非常相似的)替代方案…

>返回的字符串(例如)… Environment.getExternalStorageDirectory().getPath()“ / DressIt tops /”不代表有效目录(对于…” / DressIt Bottoms /”和/或…“ / DressIt Shoes /”).
>返回的字符串是有效路径,但其中一个或另一个是文件而不是目录.
>目录确实存在,但是您输入的目录名称有错字-请记住文件系统区分大小写.如果您将“ / DressIt tops /”目录创建为“” / dressIt tops /”(例如),则找不到有效路径.

还有一件事-永远不要假设任何路径到AndroID文件系统上的任何地方-记录Environment.getExternalStorageDirectory().getPath()实际返回的内容,检查它是否是您创建目录的位置,并检查所有内容是否正确.

总结

以上是内存溢出为你收集整理的java-使用File.listfiles()获取NullPointer异常全部内容,希望文章能够帮你解决java-使用File.listfiles()获取NullPointer异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094278.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存