不能再查看JetPack组成预览。无法实例化一个或多个类(ComposeViewAdapter)

发布于 2025-01-24 07:26:53 字数 3615 浏览 0 评论 0原文

最近,我有一个错误,可以防止任何JetPack组成的预览显示如下:

Failed to instantiate one or more classes
The following classes could not be instantiated:
     -androidx.compose.ui.tooling.ComposeViewAdapter(Open Class, Show Exception, Clear Cache)

if this is an unexpected error you can also try to build the project, then manually refresh the layout

java.lang.NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContainer
    at androidx.compose.ui.platform.ViewCompositionStrategy$DisposeOnDetachedFromWindowIfNotInPoolingContainer.installFor(ViewCompositionStrategy.android.kt:97)
    at androidx.compose.ui.platform.AbstractComposeView.<init>(ComposeView.android.kt:123)
    at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:392)
    at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:388)
    at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:131)
    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:339)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:176)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:136)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:301)
    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:965)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:363)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:739)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:895)
    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:829)

我尝试清除缓存,重建项目,按建议清洁项目,但没有任何帮助。我在Android Studio Bumblebee 2021.2.1补丁3以及Chipmunk 2021.2.1 beta 4中都遇到了相同的问题

。没有任何帮助。

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:1.1.1"
implementation "androidx.compose.material:material:1.1.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.1.1"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.1.1'

我认为这里有几个问题问类似的事情,但是它们都很老,并且使用了长期过时的作曲和Android Studio,因此它们并不是很有帮助。

I've recently got an error preventing any Jetpack Compose previews from being displayed as follows:

Failed to instantiate one or more classes
The following classes could not be instantiated:
     -androidx.compose.ui.tooling.ComposeViewAdapter(Open Class, Show Exception, Clear Cache)

if this is an unexpected error you can also try to build the project, then manually refresh the layout

java.lang.NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContainer
    at androidx.compose.ui.platform.ViewCompositionStrategy$DisposeOnDetachedFromWindowIfNotInPoolingContainer.installFor(ViewCompositionStrategy.android.kt:97)
    at androidx.compose.ui.platform.AbstractComposeView.<init>(ComposeView.android.kt:123)
    at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:392)
    at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:388)
    at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:131)
    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:339)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:176)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:136)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:301)
    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:965)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:363)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:739)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:895)
    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:829)

I have tried clearing the cache, rebuilding my project, cleaning my project as suggested but nothing has helped. I have had the same issue on both Android Studio Bumblebee 2021.2.1 Patch 3 as well as Chipmunk 2021.2.1 beta 4.

I am currently using version 1.1.1 for all my Compose related dependencies, but have tried downgrading and using newer versions but nothing helps.

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:1.1.1"
implementation "androidx.compose.material:material:1.1.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.1.1"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.1.1'

I think there are a couple of questions on here asking similar things, but they are all very old and use long outdated versions of Compose and Android Studio, so they are not very helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2025-01-31 07:26:53

这是一个已知的错误: https://issuetracker.google.google.com/issues/issues/22777777363

当前正在处理修复程序,但已经有一个解决方法:将这些依赖项添加到使用Compose Preview的每个模块中:

debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"

如果您有一个共同的Core/base/us/UI模块,则可以在此处添加并使用api 而不是实现避免将其添加到每个模块中:

debugApi "androidx.customview:customview:1.2.0-alpha01"
debugApi "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"

伪像释放参考

It is a known bug: https://issuetracker.google.com/issues/227767363

Google is currently working on a fix but there is already a workaround: add these dependencies to every module where you use the Compose preview:

debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"

If you have a common core/base/ui module, you can add it there and use Api instead of Implementation to avoid adding it to every module:

debugApi "androidx.customview:customview:1.2.0-alpha01"
debugApi "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"

Artifacts release reference

孤寂小茶 2025-01-31 07:26:53

我正在迁移一个简单的项目来撰写,并且面临着这个问题。然后我意识到我忘记了添加“ androidx.compose.ui:ui-tooling:1.1.1”

这些都是我的JetPack组成的依赖性。添加最后一行可以解决问题。花了20分钟才意识到这一点...

implementation("androidx.compose.ui:ui:1.1.1")
implementation("androidx.compose.material:material:1.1.1")
implementation("androidx.compose.ui:ui-tooling-preview:1.1.1")
debugImplementation("androidx.compose.ui:ui-tooling:1.1.1")

I was migrating one of my simple projects to compose and I faced this issue. Then I realised I forgot adding "androidx.compose.ui:ui-tooling:1.1.1"

These are all of my dependencies for jetpack compose. Adding the last line did the trick. It took 20 min to realize this...

implementation("androidx.compose.ui:ui:1.1.1")
implementation("androidx.compose.material:material:1.1.1")
implementation("androidx.compose.ui:ui-tooling-preview:1.1.1")
debugImplementation("androidx.compose.ui:ui-tooling:1.1.1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文