1

我在一个带有一些 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)
4

1 回答 1

3

您是变量 shadowing 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);  
    }
于 2013-02-27T21:58:15.670 回答