gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:58:10 26 4
gpt4 key购买 nike

我正在使用一个名为 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函数返回 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$1$1.invoke(ImageResources.kt:45)
at androidx.ui.res.ImageResourcesKt$imageResource$1$1.invoke(Unknown Source:0)
at androidx.compose.ComposerKt.remember(Composer.kt:1867)
at androidx.compose.EffectsKt$memo$2.invoke(Effects.kt:287)
at androidx.compose.EffectsKt$memo$2.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$1.invoke(ImageResources.kt:45)
at androidx.ui.res.ImageResourcesKt$imageResource$1.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$1$1$1.invoke(HomeActivity.kt:66)

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

最佳答案

更新: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 日

在撰写版本中 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()无法加载矢量资源。您需要创建一个 effectOf<VectorAsset>通过提供资源 ID 并使用 DrawVector()绘制矢量 Assets 。您可以使用下面的@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)
}
}
}

关于android - 来自 R.drawable 的 +imageResource 在 Jetpack Compose 中导致 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764407/

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