これはばかげている、私は数分で濡れたキッパーで自分自身を刺すと確信している..
私はこのメソッドを持っています。その目的は、アセット フォルダー内の特定のパスがサブフォルダーであるかどうかを判断することです。再帰検索でアセット内のファイルを検索するために使用されます。
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 でどのように表示されるかはわかりませんが、ここでは赤い線がブレークポイントで、青い線が現在の実行ポイントです。
キャッシュをクリアし、ファイル インデックスを削除し、出力フォルダーを削除して、完全な再構築を行いました。