【发布时间】:2021-11-04 23:24:14
【问题描述】:
我正在尝试将现有的 android 应用程序转换为 compose。鉴于以下依赖项,我在应用启动时和预览窗口中收到以下错误:
The following classes could not be instantiated:
- androidx.compose.ui.tooling.ComposeViewAdapter
java.lang.NoSuchMethodError: 'androidx.lifecycle.LifecycleRegistry androidx.lifecycle.LifecycleRegistry.createUnsafe(androidx.lifecycle.LifecycleOwner)'
at androidx.compose.ui.tooling.ComposeViewAdapter$FakeSavedStateRegistryOwner$1.<init>(ComposeViewAdapter.kt:705)
at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:704)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:399)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:147)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:303)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:870)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
这些是我的依赖项:
implementation "com.google.accompanist:accompanist-appcompat-theme:0.16.0"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation "androidx.compose.ui:ui:1.1.0-beta01"
implementation "androidx.compose.material:material:$rootProject.composeVersion"
implementation "androidx.compose.ui:ui-tooling-preview:$rootProject.composeVersion"
implementation "androidx.compose.ui:ui-tooling:$rootProject.composeVersion"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.runtime:runtime-livedata:$rootProject.composeVersion"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.composeVersion"
rootProject.composeVersion = "1.0.4"
我就现有问题听取了很多建议,甚至确定这不是我的 Android Studio 的问题,因为我已经能够使用 compose 创建干净的项目了。
更新(基于发布可组合代码的请求)
即使是这个可组合的代码也无法编译让我预览
@Composable
fun TestComposable() =
Text(text = "This is working.")
@Preview(showSystemUi = true, showBackground = true)
@Composable
fun TestComposablePreview() =
TestComposable()
【问题讨论】:
-
这个
androidx.compose.ui:ui:1.1.0-beta01也应该依赖于compose版本,比如androidx.compose.ui:ui:$rootProject.composeVersion -
@JüriKin 尝试改用
Preview(showSystemUi=true, showBackground=true) -
也许您应该包含您的可组合代码,以便其他人更好地理解。
-
它试图预览什么可组合代码似乎并不重要。我用上面的更新创建了一个新文件,它仍然没有编译成功。 @PhilipDukhov 我将 ui:ui 更新为 composeVersion,但这似乎不起作用。 .MARSK 我的预览中确实有这个。我在上面更新了一些仍然无法编译预览的测试可组合代码。
-
我对所有 compose 依赖项使用 1.0.1 版本。这很好用。你能试试吗?而activity-compose是1.3.0-alpha06。这两个只是我看到的区别。
标签: android kotlin android-jetpack-compose