0
 public void getSongList() {
    //retrieve song info
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        int dataColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.DATA);
        int artColumn=musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Albums.ALBUM_ART);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            String thisData = musicCursor.getString(dataColumn);

                String thisArt = musicCursor.getString(artColumn);
                File imgFile=new File(thisArt);
                if(imgFile.exists()){
                    albumArt= BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                }


            songList.add(new Song(thisId, thisTitle, thisArtist,thisData,albumArt));
        }
        while (musicCursor.moveToNext());
        musicCursor.close();
    }
}

致命例外:主要

    Process: com.example.kishan.experimentonmusicplayer, PID: 3373


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kishan.experimentonmusicplayer/com.example.kishan.experimentonmusicplayer.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                          at android.os.Looper.loop(Looper.java:207)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

原因:java.lang.IllegalStateException:无法从 CursorWindow 中读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标。

at android.database.CursorWindow.nativeGetString(Native Method)
                                                                                      at android.database.CursorWindow.getString(CursorWindow.java:438)
                                                                                      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:66)
                                                                                      at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                                                      at com.example.kishan.experimentonmusicplayer.MainActivity.getSongList(MainActivity.java:161)
                                                                                      at com.example.kishan.experimentonmusicplayer.MainActivity.onCreate(MainActivity.java:81)
                                                                                      at android.app.Activity.performCreate(Activity.java:6583)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666) 
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                      at android.os.Looper.loop(Looper.java:207) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5769) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
4

1 回答 1

0

之前发布了一个关于此问题的问题。问题出在此处:确保在从光标访问数据之前正确初始化光标。

在此链接上发布了一个有用的答案: Android光标错误-“确保在访问数据之前正确初始化光标......”

于 2017-09-11T09:18:21.550 回答