简体   繁体   English

循环文件导致Nexus 7上的力关闭,而不是SGS2?

[英]File for loop causing force close on Nexus 7, not SGS2?

When my app runs on my S2, it works perfectly with absolutely no errors, however when I try and run it on my Nexus 7 it force closes and I have no idea why it's happening on one device and not on another... 当我的应用程序在我的S2上运行时,它完全没有错误,但是当我尝试在我的Nexus 7上运行时,它会关闭,我不知道它为什么会在一台设备上发生,而不是在另一台设备上...

Here is the LogCat: 这是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

The 2 foldersize functions are: 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;
    }

and

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;
    }

Line 96: 第96行:

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

(second function) (第二功能)

Line 87: 第87行:

length += folderSize(file);

(first function) (第一功能)

I don't understand why this only happens on the one device though! 我不明白为什么这只发生在一台设备上!

EDIT1: EDIT1:

Directory declaration: 目录声明:

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

(It's already defined as a global variable) (它已被定义为全局变量)

The directory doesn't exist on your Nexus 7 and so directory.listFiles() returns null and therefore the iterator throws an NPE. 该目录在您的Nexus 7上不存在,因此directory.listFiles()返回null,因此迭代器会抛出一个NPE。

Here's what the javadoc for File.listFiles() says: 这是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. 

A simple exists() would do the job but it won't cover the case where you don't have access to the directory, which will return Null as well. 一个简单的exists()可以完成这项工作,但它不会涵盖您无法访问目录的情况,该目录也将返回Null。 So best just check for listFiles() returning null and while doing that you consolidate your two methods into one: 所以最好只检查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;
}

Now you just call this passing the directory along: 现在你只需要调用这个目录:

long size = folderSize(directory);

Also make sure you have the android.permission.READ_EXTERNAL_STORAGE permission defined in your manifest. 还要确保您在清单中定义了android.permission.READ_EXTERNAL_STORAGE权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM