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
}

編集

以下のコメントについて議論した後、プロジェクトの構造を変更する必要があると思います。

アルバムのリストを含む最初のアクティビティを追加します。1 つのアイテムをクリックすると、アルバムのトラックのリストを含む 2 番目のアクティビティが起動します。

データベースを使用すると、データのクエリと表示がより簡単になります。

于 2013-07-18T11:30:16.973 に答える