我得到了 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;在我的课堂上,我获取了设备上的所有歌曲,并使用它们填充了arraylist。
在onDestroy中,我致电:
musicList = null;
编辑
好吧,我发现了问题,当我不调用onDestroy musicList = null时,没有ConcurrentModificationException。
但是如何在onDestroy中取消引用数组列表,以便可以对其进行垃圾回收?
编辑
所以问题不是onDestroy调用,当我打开应用程序并用所有歌曲填充数组列表时发生错误,然后我关闭应用程序并重新打开它,然后引发异常。
我如何填充歌曲数组
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();
}
}
答案 0 :(得分:1)
这是一种高级方法,可让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
时,它将再次查询歌曲。