我在一个带有一些 mp3 文件的片段中创建了一个列表,然后我将此列表传递给另一个片段,它可以工作,在这个片段中,我尝试在用户按下一个时获取项目值。
以下代码行在创建列表的片段中完成这项工作,但在此片段中它会引发 IndexOutOfBoundsException。
String trackId = SD_PATH + songs.get(position);
完整代码:
public class QueueListFragment extends ListFragment {
private static final String SD_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/");
private List<String> songs = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> songList = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.playlist_item2, songs);
setListAdapter(songList);
}
public void onListReceived(List<String> songs) {
ArrayAdapter<String> songList = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.playlist_item2, songs);
setListAdapter(songList);
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long longId) {
super.onListItemClick(listView, view, position, longId);
String trackId = SD_PATH + songs.get(position);
Toast.makeText(getActivity().getApplicationContext(), trackId, Toast.LENGTH_SHORT).show();
}
}
完整的Logcat:
02-27 21:47:54.300: W/dalvikvm(3781): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-27 21:47:54.333: E/AndroidRuntime(3781): FATAL EXCEPTION: main
02-27 21:47:54.333: E/AndroidRuntime(3781): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-27 21:47:54.333: E/AndroidRuntime(3781): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-27 21:47:54.333: E/AndroidRuntime(3781): at java.util.ArrayList.get(ArrayList.java:304)
02-27 21:47:54.333: E/AndroidRuntime(3781): at com.harteg.fragmentstest.QueueListFragment.onListItemClick(QueueListFragment.java:50)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.os.Handler.handleCallback(Handler.java:725)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.os.Looper.loop(Looper.java:137)
02-27 21:47:54.333: E/AndroidRuntime(3781): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-27 21:47:54.333: E/AndroidRuntime(3781): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 21:47:54.333: E/AndroidRuntime(3781): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 21:47:54.333: E/AndroidRuntime(3781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 21:47:54.333: E/AndroidRuntime(3781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 21:47:54.333: E/AndroidRuntime(3781): at dalvik.system.NativeStart.main(Native Method)