gpt4 book ai didi

java - Android listView 在 Fragment 中点击崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:53 25 4
gpt4 key购买 nike

所以我的列表中填充了一些项目,但是每当我单击任何项​​目时它都会崩溃。无论天气如何,我是否有点击监听器,都会发生这种情况。我在网上寻找解决方案,但一直无法找出导致此问题的原因,任何帮助将不胜感激,谢谢!

 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;
}

日志猫: 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;
}

最佳答案

堆栈跟踪表明 songPicked(View) 方法已在 XML 布局中指定,但在 MainActivity 中未找到。因此,解决方案是在 MainActivity 的代码中实现它,或者从 XML 布局中的 View 中删除相应的 onClick 属性。

关于java - Android listView 在 Fragment 中点击崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com