不能再查看JetPack组成预览。无法实例化一个或多个类(ComposeViewAdapter)
最近,我有一个错误,可以防止任何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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个已知的错误: https://issuetracker.google.google.com/issues/issues/22777777363
当前正在处理修复程序,但已经有一个解决方法:将这些依赖项添加到使用Compose Preview的每个模块中:
如果您有一个共同的Core/base/us/UI模块,则可以在此处添加并使用
api 而不是
实现
避免将其添加到每个模块中:伪像释放参考
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:
If you have a common core/base/ui module, you can add it there and use
Api
instead ofImplementation
to avoid adding it to every module:Artifacts release reference
我正在迁移一个简单的项目来撰写,并且面临着这个问题。然后我意识到我忘记了添加“ androidx.compose.ui:ui-tooling:1.1.1”
这些都是我的JetPack组成的依赖性。添加最后一行可以解决问题。花了20分钟才意识到这一点...
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...