来自 R.drawable 的 +imageResource 在 Jetpack Compose 中导致 IllegalArgumentException

+imageResource from R.drawable causes IllegalArgumentException in Jetpack Compose

我正在使用名为 HomeActivity 的新空 JC activity 测试 Jetpack Compose。我正在尝试将可绘制资源加载到我的代码中。这是我的职能:

@Composable
fun home() {
    MaterialTheme(
        colors = colors,
        typography = typography
    ) {
        val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)
        val bottomActions = listOf<Image>()
        var (drawerState, onDrawerStateChangeListener) = +state { DrawerState.Closed }

        BottomDrawerLayout(drawerState = drawerState, onStateChange = onDrawerStateChangeListener, drawerContent = {
            Text(text = "Drawer")
        }) {
            Column(
                crossAxisSize = LayoutSize.Expand
            ) {
                BottomAppBar(
                    navigationIcon = {
                        drawerState = DrawerState.Opened
                        AppBarIcon(icon = menuIcon, onClick = {})
                    },
                    actionData = bottomActions,
                    action = {
                        AppBarIcon(icon = it, onClick = {})
                    }
                )
            }
        }
    }
}

根据 JC 网站,+imageResource(R.drawable.xyz) 应该将资源加载为 andX.ui Image 对象 imageResource 函数 returns Effect<Image>,它不会使应用程序崩溃,但我不知道如何将其解析为 AppBarIcon

所需的图像对象

部分 Logcat 输出:


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.craftmyspace/com.craftmyspace.activities.HomeActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bitmap
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        ...os.ZygoteInit.main(ZygoteInit.java:858)


     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter bitmap
        at androidx.ui.graphics.AndroidImage.<init>(Unknown Source:2)
        at androidx.ui.graphics.AndroidImageKt.imageFromResource(AndroidImage.kt:29)
        at androidx.ui.res.ImageResourcesKt$imageResource.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource.invoke(Unknown Source:0)
        at androidx.compose.ComposerKt.remember(Composer.kt:1867)
        at androidx.compose.EffectsKt$memo.invoke(Effects.kt:287)
        at androidx.compose.EffectsKt$memo.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.Effect.unaryPlus(Effects.kt:115)
        at androidx.ui.res.ImageResourcesKt$imageResource.invoke(ImageResources.kt:45)
        at androidx.ui.res.ImageResourcesKt$imageResource.invoke(Unknown Source:2)
        at androidx.compose.Effect.resolve(Effects.kt:106)
        at androidx.compose.Effect.resolve$default(Effects.kt:843)
        at androidx.compose.EffectsKt.unaryPlus(Effects.kt:841)
        at com.craftmyspace.activities.HomeActivityKt$home.invoke(HomeActivity.kt:66)

HomeActivity.kt:66是这个 val menuIcon = +imageResource(R.drawable.ic_baseline_menu_24)

在我的例子中,我解决了从 drawable ic_chat_bubble 资源中删除 ic_chat_bubble.xml(anydpi) 现在它工作正常。

更新:Compose_version = '1.0.0-beta01'

val painter = painterResource(id = R.drawable.img)
Image(
       painter = painter,
       contentDescription = "Profile pic",
       modifier = imageModifier,
       contentScale = ContentScale.Crop
  )

更新:2020 年 2 月 9 日

compose版本1.0.0-alpha01可以使用vectorResource(id = R.drawable.my_vector_resource_id)加载矢量资源

例如:Image(asset = vectorResource(id = R.drawable.my_vector_resource_id))Icon(asset = vectorResource(id = R.drawable.my_vector_resource_id))

旧答案

我遇到了同样的问题,观察到 imageFromResource()imageResource() 无法加载矢量资源。 您需要通过提供资源 ID 创建一个 effectOf<VectorAsset> 并使用 DrawVector() 绘制矢量资产。 您可以使用下面的@Composable函数绘制矢量资源

@Composable
fun VectorImage(@DrawableRes id: Int, tint: Color = Color.Transparent) {
    val vector = +vectorResource(id)
    WithDensity {
        Container(
            width = vector.defaultWidth.toDp(),
            height = vector.defaultHeight.toDp()
        ) {
            DrawVector(vectorImage = vector, tintColor = tint)
        }
    }
}

可以变得更容易

   Image(
    vectorResource(R.drawable.folk),
    contentScale = ContentScale.Crop
)