2013-07-18 20 views
0

我的應用程序返回媒體存儲中的相冊並將它們放置在列表視圖中。當點擊一張專輯時,會打開另一個列表視圖,其中包含與該專輯有關的歌曲。但是當我點擊任何歌曲時,應用程序崩潰。我希望能夠獲得所選歌曲的文件路徑並將其傳遞給播放該歌曲的另一個活動。以下是我的代碼: 公共類相冊擴展ListActivity { 光標遊標;如何從列表視圖獲取數據並將其傳遞給另一個活動

@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 
+0

正如我的想法:「確保光標在從它訪問數據前正確初始化。」 。爲了訪問您的光標,請嘗試使用我的代碼。你不必做一個查詢! – JJ86

回答

1

編輯你的問題,並添加logcat的。我不知道你在做什麼itemClick(糾正我,如果我不明白):你使用cursor.moveToPosition(位置),而不會確定你把光標放在哪裏;那麼你需要專欄和其他查詢人員;最後你創建一個列表。

長話短說,你似乎在創建一個列表,當你點擊一個項目! 更改代碼的方法是這樣的:

@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 
} 

編輯

我們在下面的評論經過討論,我認爲你必須改變你的項目的結構。

添加一個包含您的相冊列表的拳頭活動;當你點擊一個項目時,啓動第二個活動,這將包含專輯曲目列表。

如果使用數據庫,查詢和顯示數據將更加容易。

+0

我已經添加了Logcat。告訴我它是否闡述了我的查詢。 – diamondtrust66

+0

活動開始時,會顯示相冊列表。當我點擊某張專輯時,該專輯中的歌曲會顯示在列表視圖中(這由onListItemClick()方法處理)。如果我點擊歌曲列表中的歌曲,我想提取它的路徑並將其發送到另一個活動,但這是應用程序崩潰的地方。 – diamondtrust66

+0

@ diamondtrust66所有的操作都是在同一個活動中完成的嗎? – JJ86

相關問題