在Android中抛出空指针异常 [英] Throwing null pointer exception in Android

查看:27
本文介绍了在Android中抛出空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从音频目录中获取音频文件,为此我使用了以下代码.我无法弄清楚为什么这种情况 if (home.listFiles(new FileExtensionFilter()).length > 0) 会引发错误.代码如下.

I'm trying to fetch an audio file from the audio directory, for that I'm using below code. I'm not able to figure out why this condition if (home.listFiles(new FileExtensionFilter()).length > 0) is throwing an error. Code is given below.

Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String MEDIA_PATH = allsongsuri.toString();
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

public ArrayList<HashMap<String, String>> getPlayList(){
    System.out.println(" -- "+MEDIA_PATH);
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        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 songsList;
}

class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
    return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}

我需要使用 hashMap 将音频文件存储在 ArrayList 中.我试过这个例子将文件存储在列表中http://android-er.blogspot.in/2012/02/list-audio-media-in-mediastoreaudiomedi.html .使用这个例子,我尝试了很多将列表存储在 ArrayList 中,因为我的需求需要它.但是没有成功.我需要帮助.我必须将音频文件存储在 Arraylist 中.

I need to store audio file in ArrayList using hashMap. I tried this example to store file in List http://android-er.blogspot.in/2012/02/list-audio-media-in-mediastoreaudiomedi.html . Using this example I tried a lot to store the list in ArrayList as my reqirement need that. But didn't succeed. I need help. I have to store Audio file in Arraylist.

推荐答案

public class SongsManager {
    final String MEDIA_PATH = Environment.getExternalStorageDirectory()
            .getPath() + "/";
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String mp3Pattern = ".mp3";

    // Constructor
    public SongsManager() {

    }

    /**
     * Function to read all mp3 files and store the details in
     * ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList() {
        System.out.println(MEDIA_PATH);
        if (MEDIA_PATH != null) {
            File home = new File(MEDIA_PATH);
            File[] listFiles = home.listFiles();
            if (listFiles != null && listFiles.length > 0) {
                for (File file : listFiles) {
                    System.out.println(file.getAbsolutePath());
                    if (file.isDirectory()) {
                        scanDirectory(file);
                    } else {
                        addSongToList(file);
                    }
                }
            }
        }
        // return songs list array
        return songsList;
    }

    private void scanDirectory(File directory) {
        if (directory != null) {
            File[] listFiles = directory.listFiles();
            if (listFiles != null && listFiles.length > 0) {
                for (File file : listFiles) {
                    if (file.isDirectory()) {
                        scanDirectory(file);
                    } else {
                        addSongToList(file);
                    }

                }
            }
        }
    }

    private void addSongToList(File song) {
        if (song.getName().endsWith(mp3Pattern)) {
            HashMap<String, String> songMap = new HashMap<String, String>();
            songMap.put("songTitle",
                    song.getName().substring(0, (song.getName().length() - 4)));
            songMap.put("songPath", song.getPath());

            // Adding each song to SongList
            songsList.add(songMap);
        }
    }


}

这篇关于在Android中抛出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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