android-jetpack-compose - java.util.ConcurrentModificationException 撰写喷气背包
问题描述
我想将动态渐变应用于表面。显示新图像时,根据该图像的颜色,渐变会发生变化。就像在谷歌样本中一样,
@Composable
fun DatingHomeScreen() {
var imageId = remember { mutableStateOf(R.drawable.camelia) }
val defaultBitmap = imageResource(id = imageId.value).asAndroidBitmap()
var currentBitmap = mutableStateOf(defaultBitmap)
val swatch = generateDominantColorState(currentBitmap.value)
val dominantColors = listOf(Color(swatch.rgb), Color.Black)
Box(
modifier = boxModifier.verticalGradientBackground(
dominantColors
)
){
IconButton(
onClick = {
imageId.value = getRandomImageId()
}
)
}
}
但在第三张图片应用程序崩溃后:
java.util.concurrentmodificationexception
解决方案
getRandomImageId()
可能正在执行异步任务并更新图像的值。如果更新图像的流程一次不包含在一个实例中,则会发生异常。
看看https://developer.android.com/reference/kotlin/java/util/ConcurrentModificationException
推荐阅读
- sql - 在sql中将季度数转换为月份名称
- javascript - 如何从 promise.all 使用和返回数据
- c++ - 在 C++ 中设置对象彼此相等
- list - 在使用递归的球拍中,如果列表“L”总和为 n 但 L 中没有重复的值,我如何返回 #true
- arrays - JSON 文件的最外层可以是数组而不是对象吗?
- c# - FluentFtp 使用 tls 连接到 FTP 服务器,可以获取工作目录,但尝试获取文件列表时超时
- python - 两幅图像之间的特征匹配和检测
- javascript - 如何修复 googe places api initMap 不是函数?
- c - 将空格作为参数传递
- python-3.x - 为什么我在moviepy中创建电影的实现如此低效?