Hashmap将专辑名称与专辑名称链接起来?

时间:2018-06-29 23:35:37

标签: java sorting nullpointerexception hashmap android-cursor

我正在尝试从设备上的歌曲中显示专辑名称和专辑插图。 我尝试使用比较器对专辑名称进行排序,但是当我对专辑名称进行排序时,图稿与专辑名称不匹配。

如何将白名单与匹配的图稿链接?

我用毕加索来展示专辑封面。

我从MediaStore.Audio.Media.ALBUM_ID获得专辑封面;

然后我尝试使用哈希图将专辑名称与专辑名称链接。

但我收到此错误;

E /无法执行任务:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'boolean android.database.Cursor.moveToFirst()'

// Creating the map  "Album IDs" -> "Album Names"
    albumIdToAlbumNameMap = new HashMap<>();

    //This is what we'll ask of the albums
    String[] albumColumns = {
            SONG_ALBUMID,
            SONG_ALBUM,
    };

    // Querying the album database
    cursor = resolver.query(musicUri, albumColumns, null, null, null);

    // Iterating through the results and filling the map.
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        albumIdToAlbumNameMap.put(cursor.getString(0), cursor.getString(1));

    cursor.close();

    // Map Song IDs to Album IDs
    songIdToAlbumIdMap = new HashMap<>();

    // For each album, we'll query the databases
    for (String albumID : albumIdToAlbumNameMap.keySet()) {

        Uri uri = MediaStore.Audio.Albums.getContentUri(albumID);

        cursor = resolver.query(uri, new String[] { SONG_ID }, null, null, null);

        // Iterating through the results, populating the map
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

            long currentSongID = cursor.getLong(cursor.getColumnIndex(SONG_ID));

            songIdToAlbumIdMap.put(Long.toString(currentSongID), albumID);
        }
        cursor.close();
    }


final String musicsOnly = MediaStore.Audio.Media.IS_MUSIC + "=1";

    // Actually querying the system
    cursor = resolver.query(musicUri, columns, musicsOnly, null, null);

    if (cursor != null && cursor.moveToFirst())
    {
        // NOTE: I tried to use MediaMetadataRetriever, but it was too slow.
        //       Even with 10 songs, it took like 13 seconds,

        do {
            // Creating a song from the values on the row
            QuerySongs song = new QuerySongs(cursor.getInt(cursor.getColumnIndex(SONG_ID)),
                    cursor.getString(cursor.getColumnIndex(SONG_FILEPATH)));

            song.setTitle      (cursor.getString(cursor.getColumnIndex(SONG_TITLE)));
            song.setArtist     (cursor.getString(cursor.getColumnIndex(SONG_ARTIST)));
            song.setAlbumID    (cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID)));

            // Using the previously created genre maps
            // to fill the current song genre.
            String currentGenreID   = songIdToGenreIdMap.get(Long.toString(song.getId()));
            String currentGenreName = genreIdToGenreNameMap.get(currentGenreID);
            String currentAlbumID = songIdToAlbumIdMap.get(Long.toString(song.getId()));
            String currentAlbumName = albumIdToAlbumNameMap.get(currentAlbumID);
            song.setGenre(currentGenreName);
            song.setAlbum(currentAlbumName);

            // Adding the song to the global list
            songs.add(song);
        }
        while (cursor.moveToNext());
    }
    else
    {
        // What do I do if I can't find any songs?

    }
    cursor.close();

在这里发生错误

  // Iterating through the results, populating the map
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

            long currentSongID = cursor.getLong(cursor.getColumnIndex(SONG_ID));

            songIdToAlbumIdMap.put(Long.toString(currentSongID), albumID);
        }

1 个答案:

答案 0 :(得分:0)

似乎您需要修复查询输入,以避免游标中为null。