5

这太愚蠢了,我敢肯定我会在几分钟内用湿腌鱼刺伤自己。

我有这种方法,其目的是确定资产文件夹中的特定路径是否是子文件夹。它用于递归搜索以查找资产中的文件。

 private static boolean isDirectory(AssetManager assetManager, String path) throws     IOException
 {
    // AssetManager.list() returns a string array of assets located @ path
    // if path is a file, then the array will be empty and have zero length
    // if path does not exist, then an IOException is raised
    // (ignore the exception as in theory, this will never happen
    // since this is called by the searchAssets recursive find)

    // do nothing for uninitialised or empty paths
    if (path==null || path.equals("")){return false;}

    try {
        if (DEBUG){Log.d(TApp.APP_TAG,path + " lists " + assetManager.list(path).length + " assets");}
        if (assetManager.list(path).length > 0){
            return true;
        }
    } catch (IOException e) {
        // do nothing - path should always exist but in any case, there is nothing we can
        // do so just throw it back up
        throw e;
    }
    return false;
}

问题是它总是返回假。

当我单步执行代码时,我可以看到 .list() 从 logcat 输出和在断点处评估 .list() 为子文件夹返回一个非零值。当我逐步执行该方法时,当前执行点正确命中“返回真”;但是当我按 F7 继续时(我使用的是 IDEA),执行点会跳转到最后一条语句“return false;”,也就是返回的值。

(我不好意思问)。为什么?

[编辑] 请求显示我是如何调用它的 - 这个方法没有完成,因为我无法让上面的工作!

public static String searchAssets(AssetManager asm, String path, String filename){

    // TODO uses hard coded path separator

    // search for the file, filename, starting at path path in the assets folder
    // asm must be initialised by the caller using an application context
    // returns an empty string for non existent files or for filename = ""

    if (asm==null){return "";}

    String foundFile; // return value

    try {
        // get a list of assets located at path
        String[] files = asm.list(path);

        // files may be null if an invalid path is passed
        if (files!=null && files.length>0){

            // loop through each asset for either a subfolder to search 
            // recursively or the file we are looking for
            for (String file:files){

                // <<<<<< HERE'S THE CALL >>>>>>>
                if (isDirectory(asm,path + "/" + file)){  

                    foundFile = searchAssets(asm,file,filename); // recurse this subfolder

                    // searchAssets returns either the name of our file, if found, or an empty string 
                    if (!foundFile.equals("")){
                        return foundFile;
                    }
                } else {
                    if (file.equals(filename)){
                        return path + "/" + file;
                    }
                }
            }
        }

    } catch (IOException e) {
        // eat the exception - the caller did not set us up properly 
    }

    return "";
}

[更多编辑]

日志猫:

09-27 09:21:12.047: DEBUG/GRENDLE(2811): harmonics_data/Harmonic Data SHC lists 2 assets
09-27 09:21:12.137: DEBUG/GRENDLE(2811): harmonics_data/Harmonic Data SHC is a subfolder,     returning true

09-27 09:21:12.544: DEBUG/GRENDLE(2811): harmonics_data/Harmonic Data SHC is a not a subfolder,     returning false

这是一个屏幕截图。第一个断点(return true;)首先被命中。继续单步直接跳转到最后一条语句,return false,也就是返回的内容。这不是一个例外。异常断点永远不会被命中,我也不希望它会命中,从 logcat 中可以看出,控制流似乎是错误的。

我不知道它在 Eclipse 中的样子,但这里红线是断点,蓝线是当前执行点。

我已经清除了缓存,删除了文件索引,删除了输出文件夹并完成了完整的重建。

4

3 回答 3

1

首先,不要使用多个退出点(返回),只需创建一个布尔值 isDir 并在您的代码中相应地设置它并最终返回它。

其次,据我了解list()将返回 a String[],如果文件夹为空,它将返回null(不会进入异常,因为您没有NullPointerException从 [null].length 捕获)。

于 2012-09-27T09:09:41.397 回答
1

我真的不明白看到应用程序的日志,但我认为问题出在这里:

// <<<<<< HERE'S THE CALL >>>>>>>
if (isDirectory(asm,path + "/" + file)){

  foundFile = searchAssets(asm,path + "/" + file,filename); // recurse this subfolder

也许将路径和栏放在递归调用中可以解决您的问题,但是,无论如何,isDirectory 方法不是必需的,我将这样执行搜索方法:

public static String searchAssets(AssetManager asm, String path,
        String filename) {

    // TODO uses hard coded path separator

    // search for the file, filename, starting at path path in the assets
    // folder
    // asm must be initialized by the caller using an application context
    // returns an empty string for non existent files or for filename = ""

    if (asm == null) {
        return "";
    }

    String foundFile = ""; // return value

    try {
        // get a list of assets located at path
        String[] files = asm.list(path);

        // files may be null if an invalid path is passed
        if (files != null && files.length > 0) {

            // loop through each asset for either a subfolder to search
            // recursively or the file we are looking for
            for (String file : files) {

                foundFile = searchAssets(asm, path + "/" + file, filename); // recurse
                                                                // this
                                                                // subfolder

                // searchAssets returns either the name of our file, if
                // found, or an empty string
                if(!foundFile.equals("")){
                    return foundFile;
                }
            }
        } else {
            if (path.equals(filename)) {
                return path;
            }else{
                return "";
            }
        }

    } catch (IOException e) {
        // eat the exception - the caller did not set us up properly
    }

    return "";
}
于 2012-09-27T09:35:22.820 回答
1

我读了你的代码。很好的解释问题。我创建了相同的代码并对其进行调试,发现如果递归函数中的路径错误,则返回与您的情况相同的 FALSE,因为它无法在机器中找到该文件。

public class test {
    public static void main(String[] args) {
        listFiles("D:\\usr");
    }

    public static void listFiles(String path) {
        System.out.println("path => " + path);
        File f = new File(path);

        if (f.isDirectory()) {
            System.out.println(f.isDirectory());        
            System.out.println(f.list().length);

            for (int i = 0; i < f.list().length; i++) {
                System.out.println("file is :: " + f.list()[i]);
                listFiles(f.listFiles()[i].getAbsolutePath());          
            }
        }
    }
}

我认为唯一的问题是在 PATH 中,所以尝试将 LOG 放在每个语句中以便能够轻松调试。如果可能的话,将absolutePath传递给递归函数

谢谢你。

于 2012-09-27T09:40:14.837 回答