首页 > 解决方案 > 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

标签: android-jetpack-compose

解决方案


getRandomImageId()可能正在执行异步任务并更新图像的值。如果更新图像的流程一次不包含在一个实例中,则会发生异常。

看看https://developer.android.com/reference/kotlin/java/util/ConcurrentModificationException


推荐阅读