Fragment中的Android listView在点击时崩溃

时间:2015-05-18 00:45:54

标签: java android listview

所以我的列表中填充了一些项目,但是每当我点击任何项目时它都会崩溃。无论天气如何我都有点击监听器,都会发生这种情况。我已经在网上寻找解决方案,但是我们无法弄清楚导致这种情况的原因,非常感谢任何帮助,谢谢!

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    songList = getSongList();
    Collections.sort(songList);

    View rootView = inflater.inflate(R.layout.songs_layout, container, false);
    songListView = (ListView) rootView.findViewById(R.id.listSongs);
    songListView.setAdapter(new SongAdapter(this.getActivity(), songList));

    songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT).show();
        }
    });

    return rootView;
}

logcat的: http://pastebin.com/Zga14R5e

这里的getSongList方法:

public ArrayList<Song> getSongList() {
    ContentResolver musicResolver = this.getActivity().getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    ArrayList<Song> songList = new ArrayList<>();

    if (musicCursor != null) {
        if (musicCursor.moveToFirst()) {
            do {
                String songName = musicCursor
                        .getString(musicCursor
                                .getColumnIndex(MediaStore.Audio.Media.TITLE));

                String path = musicCursor.getString(musicCursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));


                String artistName = musicCursor.getString(musicCursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST));

                String albumName = musicCursor.getString(musicCursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));

                Song song = new Song(songName, artistName, path, albumName);
                songList.add(song);

            } while (musicCursor.moveToNext());


        }

    }

    return songList;
}

1 个答案:

答案 0 :(得分:0)

stacktrace告诉我在XML布局中指定了songPicked(View)方法,但在MainActivity中找不到。因此,解决方案是在MainActivity代码中实现它,或者从XML布局中的onClick中删除相应的View属性。