我在带有一些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)
答案 0 :(得分:3)
您是变量投影songs
,因此类范围songs
的大小始终为0
。
您声明了songs
并在顶部实例化了它:
private List<String> songs = new ArrayList<String>(); size of 0
但是,onListReceived()
还有一个名为songs
的变量,在制作适配器时使用。
你可以这样做:
String trackId = SD_PATH + listView.getItemAtPosition(position);
或
String trackId = SD_PATH + listView.getAdapter().getItem(position);
或在songs
被调用后,实际上让课程onListReceived()
指向新列表:
public void onListReceived(List<String> newSongs) {
this.songs = newSongs;
ArrayAdapter<String> songList = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.playlist_item2, songs);
setListAdapter(songList);
}