我越来越java.util.ConcurrentModificationException,我不知道为什么。
在logcat的它指向这个代码,但我没有看到任何可能导致ConcurrentModificationException的。
private void initRecyclerView() {
Main.musicList = Main.songs.songs;
Log.d(TAG, "Main.musicList: " + String.valueOf(Main.musicList));
if ((Main.musicList != null) && (!Main.musicList.isEmpty())) {
// Connects the song list to an adapter
// (Creates several Layouts from the song list)
allSongsAdapter = new AllSongsAdapter(getActivity(), Main.musicList);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewSongs.setLayoutManager(linearLayoutManager);
recyclerViewSongs.setHasFixedSize(true);
recyclerViewSongs.setAdapter(allSongsAdapter);
}
}
Main.musicList是一个公共静态的ArrayList musicList = NULL;另一个类。
而Main.songs.songs是公共ArrayList的歌曲= NULL;在我的课堂,我得到的所有设备上的歌曲,并与他们填充数组列表。
在我的onDestroy拨打:
音乐列表= NULL;
编辑
好吧,我发现这个问题,当我没有打电话的onDestroy musicList = NULL没有ConcurrentModificationException的。
但我怎么取消引用的onDestroy一个ArrayList,因此它可以被垃圾收集?
编辑
所以,问题不在于的onDestroy电话,当我打开应用程序,我的ArrayList被填充了所有歌曲时出现错误,然后我关闭应用程序,然后重新打开它,然后抛出异常。
我如何填充的歌曲阵列
songs = new ArrayList<>();
// Columns retrieved from the system database (MediaStore.Audio.Media).
String[] projection1 = {
SONG_ID,
SONG_TITLE,
SONG_ARTIST,
SONG_ALBUMID,
SONG_ALBUM,
SONG_FILEPATH,
SONG_DURATION,
SONG_YEAR,
};
// Limits results to only show MUSIC files.
// It's a SQL "WHERE" clause - it becomes `WHERE IS_MUSIC NOT EQUALS ZERO`.
final String musicsOnly = SONG_IS_MUSIC + "!=0";
// Querying the Media DATABASE.
cursor = resolver.query(musicUri, projection1, musicsOnly, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
do {
// Creating a SONG from the VALUES in each column.
Song song = new Song(cursor.getLong(cursor.getColumnIndexOrThrow(SONG_ID)),
cursor.getString(cursor.getColumnIndexOrThrow(SONG_FILEPATH)));
song.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(SONG_TITLE)));
song.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(SONG_ARTIST)));
song.setAlbumID(cursor.getLong(cursor.getColumnIndexOrThrow(SONG_ALBUMID)));
song.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(SONG_ALBUM)));
song.setDuration(cursor.getLong(cursor.getColumnIndexOrThrow(SONG_DURATION)));
song.setYear(cursor.getInt(cursor.getColumnIndexOrThrow(SONG_YEAR)));
// Using the previously created maps to add the current song GENRE.
String currentGenreID = songIdToGenreIdMap.get(Long.toString(song.getId()));
String currentGenreName = genreIdToGenreNameMap.get(currentGenreID);
song.setGenre(currentGenreName);
// Adding the Song to the global array list 'songs'.
songs.add(song);
} while (cursor.moveToNext());
}
}catch (Exception e){
// Exception caught because no songs were found.
Log.e(TAG, "Exception caught because no songs were found!", e);
throw new Exception();
}finally {
if (cursor != null ){
cursor.close();
}
}
这是一个高层次的方法,这将使GC妥善清理你的记忆。
里面你Activity
类中定义成员:
private List<Song> mMySongs;
在onCreate
方法,你初始化RecyclerView
然后读取歌曲到数组:
// getSongs is your models method where you read the songs and return them as array
mMySongs = getSongs();
// Enter code to create the adapter and set it for RecyclerView
现在您正在使用很强的参考,而不是静态的参考,当你Activity
被破坏,GC可以清理内存,当你重新启动的Activity
它将再次查询歌曲。