简体   繁体   English

为什么这个布尔方法使用错误的返回路径?

[英]Why does this boolean method use the wrong return path?

This is so stupid, I am certain I will be stabbing myself with a wet kipper in a few minutes.. 这太愚蠢了,我确信我会在几分钟内用湿腌鱼刺伤自己。

I have this method, the purpose of which is to determine if a particular path in the assets folder is a subfolder. 我有这个方法,其目的是确定assets文件夹中的特定路径是否是子文件夹。 It's used in a recursive search to find files in assets. 它用于递归搜索以查找资产中的文件。

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

The problem is that it is always returning false. 问题是它总是返回false。

When I step through the code, I can see that .list() returns a non-zero value for a subfolder both from the logcat output and from evaluating .list() at a breakpoint. 当我单步执行代码时,我可以看到.list()从logcat输出和在断点处评估.list()返回子文件夹的非零值。 When I step through the method, the current execution point correctly hits "return true;" 当我逐步执行该方法时,当前执行点正确命中“return true;” but when I hit F7 to continue (I'm using IDEA), the execution point jumps to the last statement, "return false;", which is the value returned. 但是当我按F7继续(我正在使用IDEA)时,执行点跳转到最后一个语句,“return false;”,这是返回的值。

(I'm embarrassed to be asking). (我很尴尬要问)。 Why? 为什么?

[EDIT] Request to show how I'm calling it - this method is not finished as I can't get the above to work! [编辑]请求显示我如何调用它 - 这个方法没有完成,因为我无法让上述工作!

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

[MORE EDITS] [更多编辑]

Logcat: logcat的:

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

Here's a screenshot. 这是一个截图。 The first breakpoint (return true;) is hit first. 首先击中第一个断点(return true;)。 Continue stepping jumps straight to the last statement, return false, which is what is returned. 继续直接跳转到最后一个语句,返回false,这是返回的内容。 This is NOT an exception. 这不是一个例外。 The exception breakpoint is never hit, and I don't expect it ever will, and as you can see from the logcat, the control flow seems wrong. 异常断点永远不会被击中,我不希望它会发生,并且从logcat可以看出,控制流似乎是错误的。

I don't know how it looks in Eclipse, but here the red lines are breakpoints and the blue line is the current execution point. 我不知道它在Eclipse中的外观,但这里的红线是断点,蓝线是当前的执行点。

I've cleared the caches, deleted the file index, deleted the output folders and done a complete rebuild. 我已经清除了缓存,删除了文件索引,删除了输出文件夹并完成了重建。

First, don't use multiple exit points (returns), just make a boolean isDir and set it accordingly in your code and in the end return it. 首先,不要使用多个退出点(返回),只需创建一个布尔值isDir并在代码中相应地设置它,最后返回它。

Second, as I understand list() will return a String[] and if the folder is empty it will return null (will NOT go to Exception as you don't catch NullPointerException from [null].length). 其次,正如我所理解的, list()将返回一个String[] ,如果该文件夹为空,它将返回null (因为你没有从[null] .length中捕获NullPointerException ,所以不会进入Exception)。

I really don't understand the log seeing the application, but I think that the problem is here: 我真的不明白看到应用程序的日志,但我认为问题在于:

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

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

Maybe putting the path and the bar in the recursive call solve your problem, but, anyway, the isDirectory method is not necessary, I'll do the search method this way: 也许在递归调用中放置路径和条可以解决您的问题,但是,无论如何,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 "";
}

I read your code. 我读了你的代码。 Good explanation of problem. 问题的好解释。 I Created same code and debug that and found that if path is wrong in recursive function than that returns FALSE same as in your case because it not able to locate that file in machine. 我创建了相同的代码并进行了调试,发现如果路径在递归函数中错误,则返回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());          
            }
        }
    }
}

I think only problem is with in PATH so try to put LOG in each statement so able to debug easily. 我认为只有问题在PATH中,所以尝试将LOG放在每个语句中,以便能够轻松调试。 If possible than pass absolutePath to recursive function 如果可能,请将absolutePath传递给递归函数

Thank you. 谢谢。

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

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