android studio中的渲染布局预览失败

时间:2017-09-23 13:54:09

标签: android android-layout android-appcompat

我正在使用 Android Studio 2.3.3 - Build#AI-162.4069837 ,我的build.gradle依赖块是:

compile 'com.android.support:support-v4:26.0.0-alpha1'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
compile 'com.android.support:support-vector-drawable:26.0.0-alpha1'
compile 'com.android.support:animated-vector-drawable:26.0.0-alpha1'

当我尝试在设计模式下预览布局时,视图不会在使用android.support.design.widget.CoordinatorLayoutandroid.support.v7.widget.RecyclerView或...的所有布局中呈现,但会出现以下错误 -

  

以下类无法实例化

我做了清理和重建项目,清除缓存,关闭并重新打开 Android Studio ,但没有解决问题。

java.lang.NoSuchFieldError: ViewBackgroundHelper
    at android.support.v7.widget.AppCompatBackgroundHelper.loadFromAttributes(AppCompatBackgroundHelper.java:46)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:63)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:475)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:262)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:213)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadClass(LayoutlibCallbackImpl.java:193)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:333)
    at android.view.BridgeInflater.onCreateView(BridgeInflater.java:152)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:222)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at com.android.layoutlib.bridge.bars.CustomBar.<init>(CustomBar.java:95)
    at com.android.layoutlib.bridge.bars.NavigationBar.<init>(NavigationBar.java:52)
    at com.android.layoutlib.bridge.bars.NavigationBar.<init>(NavigationBar.java:46)
    at com.android.layoutlib.bridge.impl.Layout.createNavBar(Layout.java:284)
    at com.android.layoutlib.bridge.impl.Layout.<init>(Layout.java:140)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:301)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:368)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:567)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:549)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:863)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:549)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$1(RenderTask.java:680)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:1)

将项目级build.gradle依赖项更改为此

classpath 'com.android.tools.build:gradle:2.3.3'

并用这些版本替换您的应用程序级别gradle依赖项

compile 'com.android.support:support-v4:26.+'
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:design:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-vector-drawable:26.+'
compile 'com.android.support:animated-vector-drawable:26.+'

This回答可能与您的问题有关

编辑:似乎你并不孤单。在thisthis帖子中,解决方案是等待下一次更新或在gradle.properties中禁用缓存。有些人已回滚到稳定的gradle并支持库版本作为临时解决方案。

答案 1 :(得分:0)

将主题从Theme.AppCompat.Light.DarkActionBar更改为styles.xml中的Base.Theme.AppCompat.Light.DarkActionBar对我有用。