gpt4 book ai didi

java - 当我不修改数组时出现 ConcurrentModificationException?

转载 作者:行者123 更新时间:2023-12-02 10:16:57 29 4
gpt4 key购买 nike

我收到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是一个公共(public)静态ArrayList musicList = null;在另一个类(class)。

Main.songs.songs是一个公共(public)ArrayListsongs = null;在我的类里面,我获取设备上的所有歌曲并用它们填充数组列表。

在 onDestroy 中我调用:

音乐列表=空;

编辑

好吧,我发现了问题,当我不调用 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();
}
}

最佳答案

这是一种高级方法,可以让 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 时,它会查询再次播放歌曲。

关于java - 当我不修改数组时出现 ConcurrentModificationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54619255/

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