为什么这个布尔方法使用了错误的返回路径? [英] Why does this boolean method use the wrong return path?

查看:240
本文介绍了为什么这个布尔方法使用了错误的返回路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很愚蠢的,我相信我会刺伤自己用湿腌鱼在几分钟内。

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

现在的问题是,它总是返回false。

The problem is that it is always returning false.

当我通过code步骤,我可以看到.LIST()返回无论是从logcat的输出和评估.LIST()在断点处子文件夹一个非零值。当我在方法步骤,当前执行点准确命中返回true;但是当我打F7继续(我使用IDEA),执行点跳转到最后一条语句,返回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. When I step through the method, the current execution point correctly hits "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.

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

(I'm embarrassed to be asking). Why?

请求来说明如何我打电话了! - 这种方法还没有完成,因为我不能得到上述工作

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的:

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

下面是截图。第一个断点(返回true;)首先击中。继续步跳跃直奔最后一条语句,返回false,这是返回什么。这是不是一个例外。唯一的例外断点从不打的,我不希望它永远会和你可以从看到的logcat,控制流似乎是错误的。

Here's a screenshot. The first breakpoint (return true;) is hit first. Continue stepping jumps straight to the last statement, return false, which is what is returned. 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.

我不知道它的外观在Eclipse,但这里的红线是断点和蓝线是当前执行点。

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.

我已经清除缓存,删除文件索引,删除输出文件夹,做一个完整的重建。

I've cleared the caches, deleted the file index, deleted the output folders and done a complete rebuild.

推荐答案

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

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

也许把路径和酒吧递归调用解决你的问题,但是,不管怎么说,isDirectory方法是没有必要的,我会做的搜索方法是这样的:

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:

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

这篇关于为什么这个布尔方法使用了错误的返回路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆