提问人:Ihor 提问时间:7/11/2023 最后编辑:engIhor 更新时间:10/18/2023 访问量:522
预览 java.lang.NoSuchMethodError:androidx.compose.foundation.layout.SizeKt.fillMaxWidth$default
Preview java.lang.NoSuchMethodError: androidx.compose.foundation.layout.SizeKt.fillMaxWidth$default
问:
我开始将我们的代码迁移到 Compose,但@Preview有问题。
例如,当我在没有默认参数“fraction = 1f”的情况下使用时,我有渲染问题modifier.fillMaxWidth()
java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.SizeKt.fillMaxWidth$default(androidx.compose.ui.Modifier, java.lang.Float, int, java.lang.Object)'
或使用不带底部参数的填充
.padding(
top = dimensionResource(id = R.dimen._4sdp),
start = dimensionResource(id = R.dimen._16sdp),
end = dimensionResource(id = R.dimen._16sdp)
)
java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.PaddingKt.padding-qDBjuR0$default(androidx.compose.ui.Modifier, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, int, java.lang.Object)'
这是我使用的代码
@Composable
fun Info(
modifier: Modifier = Modifier,
step: String,
stepInfo: String,
) {
Column(modifier = modifier.fillMaxWidth(fraction = 1f)) {
Text(
text = step,
modifier = Modifier
.background(
color = colorResource(id = R.color.color_on_background_variant3),
shape = RoundedCornerShape(90)
)
.size(dimensionResource(id = R.dimen._28sdp))
.padding(dimensionResource(id = R.dimen._6sdp))
.align(Alignment.CenterHorizontally),
textAlign = TextAlign.Center
)
Text(
text = stepInfo,
textAlign = TextAlign.Center,
modifier = Modifier
.padding(
top = dimensionResource(id = R.dimen._4sdp),
start = dimensionResource(id = R.dimen._16sdp),
end = dimensionResource(id = R.dimen._16sdp),
bottom = 0.dp
)
.align(Alignment.CenterHorizontally),
)
}
}
@Preview
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, name = "InfoPreviewNight")
@Composable
fun InfoPreview() {
MdcTheme {
Info(
step = "1",
stepInfo = ".............."
)
}
}
buidld.gradle (:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.firebase.appdistribution'
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.1'
}
dependencies {
implementation platform('androidx.compose:compose-bom:2022.10.00')
implementation "androidx.compose.runtime:runtime"
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.foundation:foundation"
implementation "androidx.compose.foundation:foundation-layout"
implementation "androidx.compose.material:material"
implementation "androidx.compose.runtime:runtime-livedata"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "com.google.accompanist:accompanist-themeadapter-material:0.30.1"
debugImplementation "androidx.compose.ui:ui-tooling"
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
}
Gradle 插件版本 ------ 7.2.2 Gradle 版本 ------- 7.3.3
我尝试更改 kotlinCompilerExtensionVersion,但没有得到任何结果 和 stackoverflow 问题无济于事。
答:
我遇到了同样的问题,我将 Compose 编译器升级到最新版本,现在预览版再次正常工作。
在 build.gradle 中
composeOptions {
//find latest compiler version here: https://developer.android.com/jetpack/androidx/releases/compose-compiler
kotlinCompilerExtensionVersion "1.5.3"
kotlinCompilerVersion kotlin_version
}
评论
将 Android Studio 更新到 Giraffe |2022.3.1 补丁 1 版本,broblem 已经消失了
评论
2023.06.01
1.4.1
1.8.0
8.0