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

問題は、常に false を返すことです。

コードをステップ実行すると、logcat 出力とブレークポイントでの .list() の評価の両方から、.list() がサブフォルダーに対してゼロ以外の値を返すことがわかります。メソッドをステップ実行すると、現在の実行ポイントが「return true;」に正しくヒットします。しかし、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;) が最初にヒットします。ステッピング ジャンプを続行して最後のステートメントに直接ジャンプし、false を返します。これが返されます。これは例外ではありません。例外ブレークポイントがヒットすることは決してありません。ログキャットからわかるように、制御フローが間違っているようです。

Eclipse でどのように表示されるかはわかりませんが、ここでは赤い線がブレークポイントで、青い線が現在の実行ポイントです。

キャッシュをクリアし、ファイル インデックスを削除し、出力フォルダーを削除して、完全な再構築を行いました。

4

3 に答える 3

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 に答える