[英]Music player app crashes attempting to crawl subfolders looking for audio files
I have a working music player app, but so far the app is only set to look for audio files in the top level folder that the user designates. 我有一个正在运行的音乐播放器应用程序,但到目前为止,该应用程序仅设置为在用户指定的顶级文件夹中查找音频文件。 I would like to look into subfolders for audio files at least 1 level deep as well.
我也想研究至少1级深度的音频文件的子文件夹。
My code attempt to look for audio files in subfolders: 我的代码尝试在子文件夹中查找音频文件:
//search subfolders of root media directory for audio files
File[] someFiles = home.listFiles();
for (int i = 0; i < someFiles.length; i++) {
File selectedFile = someFiles[i];
if (selectedFile.isDirectory()) {
searchForAudioFiles(selectedFile, uniqueSongIDInt);
}
}
Unfortunately I am getting a null pointer exception. 不幸的是,我得到了一个空指针异常。 I ran debugger to try to figure out what variable is null, but haven't yet figured that out.
我运行调试器尝试找出什么变量为null,但尚未弄清楚。
Logcat: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshbgold.simplemusicplayer/com.joshbgold.simplemusicplayer.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) at android.app.ActivityThread.access$700(ActivityThread.java:158) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5365) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at
com.joshbgold
Logcat: java.lang.RuntimeException:无法启动活动ComponentInfo {com.joshbgold.simplemusicplayer / com.joshbgold.simplemusicplayer.MainActivity}: android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)上的
java.lang.NullPointerException android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1296)上的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)在android.app.ActivityThread.access $ 700(ActivityThread.java:158)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1296)在android。 os.Handler.dispatchMessage(Handler.java:99)位于android.os.Looper.loop(Looper.java:176)位于android.app.ActivityThread.main(ActivityThread.java:5365)在java.lang.reflect.Method com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)处java.lang.reflect.Method.invoke(Method.java:511)处的.invokeNative(Native方法)在
com.joshbgold显示java.lang.NullPointerException:.os.ZygoteInit.main(ZygoteInit.java:869)在dalvik.system.NativeStart.main(本机方法)所造成的
.simplemusicplayer.SongsManager.searchForAudioFiles(SongsManager.java:66) at com.joshbgold.simplemusicplayer.SongsManager.getPlayList(SongsManager.java:56) at com.joshbgold.simplemusicplayer.MainActivity.onCreate(MainActivity.java:126)
com.joshbgold.simplemusicplayer.SongsManager.getPlayList(SongsManager.java:56)上的.simplemusicplayer.SongsManager.searchForAudioFiles(SongsManager.java:66),com.joshbgold.simplemusicplayer.MainActivity.onCreate(MainActivity.java:126)
Here is line 66, that the Logcat seems to indicate may be causing the exception: 这是第66行,Logcat似乎表明这可能是导致异常的原因:
if ((home.listFiles(new FileExtensionFilter()).length > 0)) {
Here is the complete SongsManager class: 这是完整的SongsManager类:
import android.content.Context;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
public class SongsManager {
public HashMap<String, String> song;
public String songTitle;
private String uniqueSongIDString = "0";
public Context mContext;
protected String lowerCaseName = "";
// SDCard Path
public String MEDIA_PATH = "/storage";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<>();
private ArrayList<HashMap<String, String>> filteredSongsList = new ArrayList<>();
public SongsManager(Context context, String folderPath) {
mContext = context;
if (folderPath != null && !"".equals(folderPath)) {
MEDIA_PATH = folderPath;
}
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
*/
public ArrayList<HashMap<String, String>> getPlayList() {
File home = new File(MEDIA_PATH);
uniqueSongIDString = "0";
int uniqueSongIDInt = 0;
if (songsList != null) {
songsList.clear();
}
if (home.listFiles() != null) {
//search audio files in root media directory user has selected
//searchForAudioFiles(home, uniqueSongIDInt);
//search subfolders of root media directory for audio files
File[] someFiles = home.listFiles();
for (int i = 0; i < someFiles.length; i++) {
File selectedFile = someFiles[i];
if (selectedFile.isDirectory()) {
searchForAudioFiles(selectedFile, uniqueSongIDInt);
}
}
}
// return songs playlist_item array
return songsList;
}
private void searchForAudioFiles(File home, int uniqueSongIDInt) {
if ((home.listFiles(new FileExtensionFilter()).length > 0)) {
for (File file : home.listFiles(new FileExtensionFilter())) { //for each file that is an audio file in home directory
song = new HashMap<>(); //make a hashmap data structure to store song info
songTitle = file.getName();
//remove track numbers from song titles
songTitle = songTitle.replaceFirst("^\\d*\\s", ""); //replaces leading digits & following space
songTitle = songTitle.replaceFirst("^\\d*\\-\\d*", ""); //replaces leading digits, following hyphen, and following digits
song.put("songTitle", songTitle);
song.put("songPath", file.getPath());
song.put("songUniqueID", uniqueSongIDString);
uniqueSongIDInt++;
uniqueSongIDString = String.valueOf(uniqueSongIDInt);
// Adding each song to SongList
songsList.add(song);
}
}
}
/**
* Class to filter files which are having .mp3 extension
*/
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
lowerCaseName = name.toLowerCase();
return (name.endsWith(".mp3") || name.endsWith(".wma") || name.endsWith(".wav") || name.endsWith(".m4a") || name.endsWith(".flac"));
}
}
// Filter Class
public ArrayList<HashMap<String, String>> filter(String searchString) {
searchString = searchString.toLowerCase(Locale.getDefault());
songsList.clear();
songsList = getPlayList();
//searchString is empty, so show all songs in results
if (searchString.length() == 0) {
if (filteredSongsList != null) {
filteredSongsList.clear();
}
filteredSongsList = songsList;
}
//only return songs that match the search string
else {
if (filteredSongsList != null) {
filteredSongsList.clear();
}
for (HashMap<String, String> song : songsList) {
if (song != null) {
String songTitle = song.get("songTitle");
if (songTitle.toLowerCase().contains(searchString)) {
filteredSongsList.add(song);
}
}
}
}
return filteredSongsList;
}
}
You can clone the whole project from Github here 您可以在此处从Github克隆整个项目
The javadoc for listFiles() makes it clear that it can return null when the directory is empty. listFiles()的javadoc清楚表明,当目录为空时,它可以返回null。 So you should check for and handle this case.
因此,您应该检查并处理这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.