Android错误:此代码有什么问题?

时间:2015-11-19 10:43:55

标签: android media-player android-mediaplayer

在onItemclicklistener的onclick方法中,我试图获取已被选中的项的id。我认为这是错误的。这样做的正确方法是什么?我想稍后将此链接到另一个将播放所选歌曲的活动。使用明确的意图是最好的方法吗?

public class SongListFragment extends Fragment {

private int audioIndex;
public SongListFragment() { }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_songs, container, false);
    ListView audioView = (ListView)view.findViewById(R.id.song_view);
    final ArrayList<String> audioList = new ArrayList<>();
    String[] proj = {MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DISPLAY_NAME};
    final Cursor audioCursor = getActivity().
                            getContentResolver().
                            query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);

    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            do {
                int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                audioList.add(audioCursor.getString(audioIndex));
            } while (audioCursor.moveToNext());
        }
    }


    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                                                android.R.layout.simple_list_item_1,
                                                android.R.id.text1, audioList);
    audioView.setAdapter(adapter);
    audioView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String[] proj = {MediaStore.Audio.Media.DISPLAY_NAME};
            final Cursor audioCursor = getActivity().
                    getContentResolver().
                    query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);

            if (audioCursor != null) {
                if (audioCursor.moveToFirst()) {
                    do {
                        int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                    } while (audioCursor.moveToNext());}}
            audioCursor.close();
            Toast.makeText(getActivity().getApplicationContext(), R.string.select+audioCursor.getString(audioIndex), Toast.LENGTH_SHORT).show();
        }
    });
    return view;

}
}

这是logcat:

11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime: FATAL EXCEPTION: main
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime: Process: com.bignerdranch.android.songretrieve, PID: 5832
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime: android.database.CursorIndexOutOfBoundsException: Index 2082 requested, with a size of 2082
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at com.bignerdranch.android.songretrieve.SongListFragment$1.onItemClick(SongListFragment.java:72)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.widget.AdapterView.performItemClick(AdapterView.java:300)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.widget.AbsListView$3.run(AbsListView.java:3833)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5312)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
11-19 17:05:08.108 5832-5832/com.bignerdranch.android.songretrieve E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
11-19 17:05:09.742 5832-5832/com.bignerdranch.android.songretrieve I/Process: Sending signal. PID: 5832 SIG: 9

2 个答案:

答案 0 :(得分:0)

请更改此行:

int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)-1;

似乎你得到一个超出范围的异常,因为它从1开始而不是0。

答案 1 :(得分:0)

你的onItem点击监听器应该是这样的:

audioView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           int clickedAudioIndex = Integer.parseInt(audioList.get(position));
            Toast.makeText(getActivity().getApplicationContext(), R.string.select+audioCursor.getString(clickedAudioIndex), Toast.LENGTH_SHORT).show();
        } 
    });