0

我的应用程序从媒体商店返回专辑并将它们放在列表视图中。单击专辑时,它会打开另一个列表视图,其中包含与该专辑有关的歌曲。但是当我点击任何歌曲时,应用程序崩溃了。我希望能够获取所选歌曲的文件路径并将其传递给播放该歌曲的另一个活动。这是我的代码: public class Albums extends ListActivity { Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
            android.provider.MediaStore.Audio.Albums.ALBUM };

    cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            columns, null, null, null);

    String[] displayFields = new String[] { MediaStore.Audio.Albums.ALBUM };
    int[] displayViews = new int[] { android.R.id.text1 };
    setListAdapter(new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, cursor, displayFields,
            displayViews));

}

protected void onListItemClick(ListView l, View v, int position, long id) {
    if (cursor.moveToPosition(position)) {

        String[] columns = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE, };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";

        String whereVal[] = { cursor.getString(cursor
                .getColumnIndex(MediaStore.Audio.Albums.ALBUM)) };

        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                columns, where, whereVal, orderBy);

        String[] displayFields = new String[] { MediaStore.Audio.Media.DISPLAY_NAME };
        int[] displayViews = new int[] { android.R.id.text1 };

        setListAdapter(new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, cursor, displayFields,
                displayViews));

    }
}


}

这是 Logcat 中包含的内容:

07-18 17:44:20.327: D/libEGL(30458): loaded /system/lib/egl/libGLES_android.so
07-18 17:44:20.387: D/libEGL(30458): loaded /system/lib/egl/libEGL_adreno200.so
07-18 17:44:20.407: D/libEGL(30458): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
07-18 17:44:20.437: D/libEGL(30458): loaded /system/lib/egl/libGLESv2_adreno200.so
07-18 17:44:20.607: I/Adreno200-EGLSUB(30458): <ConfigWindowMatch:2078>: Format RGBA_8888.
07-18 17:44:20.667: D/memalloc(30458): ashmem: Mapped buffer base:0x51fcf000 size:1536000 fd:62
07-18 17:44:20.697: D/OpenGLRenderer(30458): Enabling debug mode 0
07-18 17:44:20.818: D/memalloc(30458): ashmem: Mapped buffer base:0x52156000 size:1536000 fd:65
07-18 17:44:40.737: D/OpenGLRenderer(30458): has fontRender patch
07-18 17:44:41.047: D/memalloc(30458): ashmem: Mapped buffer base:0x526bd000 size:1536000 fd:68
07-18 17:44:43.750: I/Adreno200-EGLSUB(30458): <ConfigWindowMatch:2078>: Format RGBA_8888.
07-18 17:44:43.770: D/memalloc(30458): ashmem: Mapped buffer base:0x52834000 size:1536000 fd:71
07-18 17:44:43.960: D/memalloc(30458): ashmem: Mapped buffer base:0x52bab000 size:1536000 fd:78
07-18 17:44:43.980: D/OpenGLRenderer(30458): Flushing caches (mode 0)
07-18 17:44:44.981: D/memalloc(30458): ashmem: Mapped buffer base:0x51ecf000 size:1536000 fd:63
07-18 17:44:47.454: E/CursorWindow(30458): Failed to read row 4, column -1 from a CursorWindow which has 11 rows, 5 columns.
07-18 17:44:47.454: D/AndroidRuntime(30458): Shutting down VM
07-18 17:44:47.454: W/dalvikvm(30458): threadid=1: thread exiting with uncaught exception (group=0x40ac7228)
07-18 17:44:47.494: E/AndroidRuntime(30458): FATAL EXCEPTION: main
07-18 17:44:47.494: E/AndroidRuntime(30458): java.lang.IllegalStateException: Couldn't read row 4, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWindow.nativeGetString(Native Method)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWindow.getString(CursorWindow.java:491)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.database.CursorWrapper.getString(CursorWrapper.java:118)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.project.helixplayer.Albums.onListItemClick(Albums.java:49)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.app.ListActivity$2.onItemClick(ListActivity.java:326)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView.performItemClick(AbsListView.java:1077)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2533)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.widget.AbsListView$1.run(AbsListView.java:3198)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Handler.handleCallback(Handler.java:605)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.os.Looper.loop(Looper.java:154)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at android.app.ActivityThread.main(ActivityThread.java:4945)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 17:44:47.494: E/AndroidRuntime(30458):    at dalvik.system.NativeStart.main(Native Method)
07-18 17:44:49.846: D/Process(30458): killProcess, pid=30458
07-18 17:44:49.846: D/Process(30458): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-18 17:44:49.846: D/Process(30458): java.lang.Thread.getStackTrace(Thread.java:599)
07-18 17:44:49.846: D/Process(30458): android.os.Process.killProcess(Process.java:788)
07-18 17:44:49.846: D/Process(30458): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
07-18 17:44:49.846: D/Process(30458): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
07-18 17:44:49.846: D/Process(30458): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
07-18 17:44:49.846: D/Process(30458): dalvik.system.NativeStart.main(Native Method)
07-18 17:44:49.846: I/Process(30458): Sending signal. PID: 30458 SIG: 9
4

1 回答 1

1

编辑您的问题并添加 LogCat。我不知道你在 itemClick 上做了什么(如果我不明白,请纠正我):你使用 cursor.moveToPosition(position) 而不确定你把光标放在哪里;然后你拿专栏和其他查询人员;最后你创建一个列表。

长话短说,当您单击一个项目时,您似乎正在创建一个列表!以这种方式更改方法上的代码:

@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
    // Get the cursor, positioned to the corresponding row in the result set
    Cursor cursor = (Cursor) listViev.getItemAtPosition(position);
    // Find your data on cursor and launch your second activity in order to show album's tracks
}

编辑

在我们讨论了下面的评论之后,我认为你必须改变你的项目结构。

添加一个包含您的专辑列表的拳头活动;当您单击一个项目时,启动第二个活动,其中将包含专辑曲目列表。

如果使用数据库,查询和显示数据会更容易。

于 2013-07-18T11:30:16.973 回答