我正在尝试制作音乐播放器但是从SD卡获取歌曲列表时出现问题。 它显示了这个错误:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
这是SongManager
代码,此类应返回歌曲列表
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/sound_recorder");
//final String MEDIA_PATH = Environment.getExternalStorageDirectory()+"/sound_recorder";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Constructor
public SongsManager(){
}
/**
* Function to read all mp3 files from sdcard
* and store the details in ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList(){
Log.d("testsd", MEDIA_PATH);
File home = new File(MEDIA_PATH);
// if (home.listFiles(new FileExtensionFilter()).length > 0) //don't use this to avoid null pointer exception !
if (home.listFiles(new FileExtensionFilter())!=null) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3") || name.endsWith(".3gpp") || name.endsWith(".wav")); //add more extensions here
}
}
}
答案 0 :(得分:-1)
您的数组访问代码正在尝试访问在为数组列表指定的范围内不存在的元素,调试从数组中访问的最后一个元素将在这方面帮助您