0

当我的应用程序在我的 S2 上运行时,它可以完美运行,绝对没有错误,但是当我尝试在我的 Nexus 7 上运行它时,它会强制关闭,我不知道为什么它发生在一台设备上而不是另一台设备上......

这是LogCat:

07-03 18:33:29.139: E/AndroidRuntime(11990): FATAL EXCEPTION: main
07-03 18:33:29.139: E/AndroidRuntime(11990): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamw.root.logeraser/com.liamw.root.logeraser.MainActivity}: java.lang.NullPointerException
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.os.Looper.loop(Looper.java:137)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread.main(ActivityThread.java:5229)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at java.lang.reflect.Method.invoke(Method.java:525)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at dalvik.system.NativeStart.main(Native Method)
07-03 18:33:29.139: E/AndroidRuntime(11990): Caused by: java.lang.NullPointerException
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.liamw.root.logeraser.FolderTools.folderSize(FolderTools.java:96)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.liamw.root.logeraser.FolderTools.folderSize(FolderTools.java:87)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.liamw.root.logeraser.MainActivity.initialize(MainActivity.java:194)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at com.liamw.root.logeraser.MainActivity.onCreate(MainActivity.java:131)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.Activity.performCreate(Activity.java:5167)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-03 18:33:29.139: E/AndroidRuntime(11990):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
07-03 18:33:29.139: E/AndroidRuntime(11990):    ... 11 more

2个文件夹大小函数是:

public long folderSize() {
        long length = 0;

        for (File file : directory.listFiles()) {
            if (file.isFile())
                length += file.length();
            else if (file != null)
                length += folderSize(file);
        }

        return length;
    }

public long folderSize(File directory) {
        long length = 0;

        for (File file : directory.listFiles()) {
            if (file.isFile())
                length += file.length();
            else if (file != null)
                length += folderSize(file);
        }

        return length;
    }

第 96 行:

for (File file : directory.listFiles()) { 

(第二个功能)

第 87 行:

length += folderSize(file);

(第一个功能)

我不明白为什么这只发生在一台设备上!

编辑1:

目录声明:

directory = new File("/data/log");

(它已经定义为全局变量)

4

1 回答 1

2

该目录在您的 Nexus 7 上不存在,因此 directory.listFiles() 返回 null,因此迭代器会引发 NPE。

这是 File.listFiles() 的 javadoc 所说的:

An array of abstract pathnames denoting the files and directories
in the directory denoted by this abstract pathname.
The array will be empty if the directory is empty.
Returns null if this abstract pathname does not denote a directory,
or if an I/O error occurs.

一个简单的 exists() 可以完成这项工作,但它不会涵盖您无权访问目录的情况,这也会返回 Null。所以最好只检查 listFiles() 返回 null 并在这样做的同时将两种方法合并为一个:

public long folderSize(File directory) {
    long length = 0;
    File[] files = directory.listFiles();
    if (files != null) {
        for (File file : files) {
            length += file.isFile() ? file.length() : folderSize(file);
        }
    }
    return length;
}

现在你只需调用这个传递目录:

long size = folderSize(directory);

还要确保您的清单中定义了 android.permission.READ_EXTERNAL_STORAGE 权限。

于 2013-07-03T17:55:52.240 回答