Android Studio:使用CoordinatorLayout渲染问题

时间:2021-07-01 20:53:04

When I set a CoordinatorLayout as my root element I get the following error when Android Studio is rendering a preview.

当我将CoordinatorLayout设置为我的根元素时,我在Android Studio渲染预览时会出现以下错误。

java.lang.NoClassDefFoundError: Could not initialize class android.support.design.widget.CoordinatorLayout
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
    at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:177)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:214)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:142)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:413)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:321)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:497)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:485)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:894)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:485)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:590)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:644)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:79)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:586)
    at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:581)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

My layout file looks like this

我的布局文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/detail_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/appbar_layout"
        android:layout_marginTop="-25dp"
        android:background="@color/background_light"
        android:paddingTop="36dp"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@id/appbar_layout"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="-32dp"
        android:src="@android:drawable/ic_menu_call"
        app:layout_scrollFlags="scroll|exitUntilCollapsed" />
</android.support.design.widget.CoordinatorLayout>

Here is my build.gradle

这是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.godine"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}  

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'

    ...
}

Does anyone have an idea what might be wrong here?

有谁知道这里可能有什么问题?

Edit As @deniz already mentioned in an answer the preview works with the newest update of the Android Studio

编辑为@deniz在答案中已经提到,预览适用于Android Studio的最新更新

4 个解决方案

#1


15  

This is a known issue in the Android Open Source Project Issue Tracker:

这是Android开源项目问题跟踪器中的已知问题:

https://code.google.com/p/android/issues/detail?id=175595

The unfortunate workaround described by the Android project member is:

Android项目成员描述的不幸解决方法是:

Workarounds: Try rendering with M preview using AS v1.3 preview OR use FrameLayout for designing and change to android.support.design.widget.CoordinatorLayout while debugging.

解决方法:尝试使用AS v1.3预览进行M预览渲染或使用FrameLayout进行设计,并在调试时更改为android.support.design.widget.CoordinatorLayout。

#2


3  

Update android studio to latest version (beta) 1.3 beta version.Menu -> 'Check for updates'

将android studio更新到最新版本(beta)1.3 beta版.Menu - >'检查更新'

If you are not able to find the beta update change android studio settings(Updates), then update that patch and render with M preview

如果您无法找到beta更新更改android studio设置(更新),则更新该补丁并使用M预览进行渲染

Android Studio:使用CoordinatorLayout渲染问题

Try Renderring your preview using Android M preview.

尝试使用Android M预览渲染预览。

Android Studio:使用CoordinatorLayout渲染问题

#3


3  

I solved the similar error of android...coordinatorlayout that appear instead of the preview of android app by:

我解决了类似的错误android ... coordinatorlayout出现而不是Android应用程序的预览:

1- Close the project

1-关闭项目

2- Restart the android studio

2-重启android studio

3- Build the project

3-构建项目

May be it will help someone.

可能会帮助别人。

#4


2  

Wait! Do not switch to beta channel.

等待!不要切换到测试频道。

As of July 29, Android Studio 1.3 has been released on stable channel. I checked, and can confirm that the new version of the IDE completely addresses your issue in question with CoordinatorLayout rendering.

截至7月29日,Android Studio 1.3已在稳定频道上发布。我检查过,并且可以确认IDE的新版本通过CoordinatorLayout渲染完全解决了您的问题。

Android Studio 1.3 is not yet published on the Android website, nonetheless, you can upgrade your Android Studio from 1.2 to 1.3 by downloading the update patch through the IDE menu. Simply click on:

Android Studio 1.3尚未在Android网站上发布,但您可以通过IDE菜单下载更新补丁,将Android Studio从1.2升级到1.3。只需点击:

Help --> Check for Update --> Update and Restart

帮助 - >检查更新 - >更新并重新启动

P.S. You do not need Android M to render either. Lower APIs work just as well.

附:您也不需要Android M来渲染。较低的API也可以正常工作。

#1


15  

This is a known issue in the Android Open Source Project Issue Tracker:

这是Android开源项目问题跟踪器中的已知问题:

https://code.google.com/p/android/issues/detail?id=175595

The unfortunate workaround described by the Android project member is:

Android项目成员描述的不幸解决方法是:

Workarounds: Try rendering with M preview using AS v1.3 preview OR use FrameLayout for designing and change to android.support.design.widget.CoordinatorLayout while debugging.

解决方法:尝试使用AS v1.3预览进行M预览渲染或使用FrameLayout进行设计,并在调试时更改为android.support.design.widget.CoordinatorLayout。

#2


3  

Update android studio to latest version (beta) 1.3 beta version.Menu -> 'Check for updates'

将android studio更新到最新版本(beta)1.3 beta版.Menu - >'检查更新'

If you are not able to find the beta update change android studio settings(Updates), then update that patch and render with M preview

如果您无法找到beta更新更改android studio设置(更新),则更新该补丁并使用M预览进行渲染

Android Studio:使用CoordinatorLayout渲染问题

Try Renderring your preview using Android M preview.

尝试使用Android M预览渲染预览。

Android Studio:使用CoordinatorLayout渲染问题

#3


3  

I solved the similar error of android...coordinatorlayout that appear instead of the preview of android app by:

我解决了类似的错误android ... coordinatorlayout出现而不是Android应用程序的预览:

1- Close the project

1-关闭项目

2- Restart the android studio

2-重启android studio

3- Build the project

3-构建项目

May be it will help someone.

可能会帮助别人。

#4


2  

Wait! Do not switch to beta channel.

等待!不要切换到测试频道。

As of July 29, Android Studio 1.3 has been released on stable channel. I checked, and can confirm that the new version of the IDE completely addresses your issue in question with CoordinatorLayout rendering.

截至7月29日,Android Studio 1.3已在稳定频道上发布。我检查过,并且可以确认IDE的新版本通过CoordinatorLayout渲染完全解决了您的问题。

Android Studio 1.3 is not yet published on the Android website, nonetheless, you can upgrade your Android Studio from 1.2 to 1.3 by downloading the update patch through the IDE menu. Simply click on:

Android Studio 1.3尚未在Android网站上发布,但您可以通过IDE菜单下载更新补丁,将Android Studio从1.2升级到1.3。只需点击:

Help --> Check for Update --> Update and Restart

帮助 - >检查更新 - >更新并重新启动

P.S. You do not need Android M to render either. Lower APIs work just as well.

附:您也不需要Android M来渲染。较低的API也可以正常工作。