Error inflating class CollapsingToolbarLayout

2019-01-11 03:49发布

问题:

My CollapsingToolbarLayout crashes because of Error inflating class android.support.design.widget.CollapsingToolbarLayout.

I updated the android studio last weekend. Before the updated, it works fine. After I updated it, it crashed. However, I tried to downgrade it to 2.0.0 version and it's not working anymore. My coworker has no problem with the CollapsingToolbarLayout. I am not sure what happened.

The error was:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.android/com.test.android.ui.activity.RandomActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                   Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
                                                                      at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                      at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
                                                                      at com.test.android.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31)
                                                                      at android.app.Activity.performCreate(Activity.java:5990)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                                                                   Caused by: java.lang.reflect.InvocationTargetException
                                                                      at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                      at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                                                                      at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                      at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) 
                                                                      at com.test.android.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31) 
                                                                      at android.app.Activity.performCreate(Activity.java:5990) 
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                                                                   Caused by: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'android.support.v4.graphics.drawable.DrawableCompat' appears in /data/app/com.test.android-1/base.apk)
                                                                      at android.support.design.widget.CollapsingToolbarLayout.setStatusBarScrim(CollapsingToolbarLayout.java:663)
                                                                      at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:197)
                                                                      at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:132)
                                                                      at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                                      at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                                                                      at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                      at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) 
                                                                      at com.test.android.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31) 
                                                                      at android.app.Activity.performCreate(Activity.java:5990) 
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at java.lang.reflect.Method.invoke(Method.java:372)

Here's my CollapsingToolbarLayout:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:clickable="true"
            app:contentScrim="@color/blue"
            app:statusBarScrim="@color/blue_dark"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            >

            <include layout="@layout/layout_page" />

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

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

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

    <include
        layout="@layout/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

Here's my gradle file:

compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:support-v13:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:customtabs:23.4.0'
compile 'com.android.support:design:23.4.0'

Do you guys have any idea? Thanks.

回答1:

I found a solution May it work try it:

add below code in gradle build file

compile ('com.android.support:support-v4:23.4.0'){
    force = true;
}

Seems like it is having version conflict issue. All support library must be of same version. However, I didn't use v4 support library before and it works. I don't know why updatimg android studio to 2.1.1 causes the issue. I just force all v4 library to the same as other support libraries.



回答2:

I know there is an accepted answer already, BUT it is not working (or no longer working).

I spent a couple of hours researching on this error, what I came up was this.

First, due to initial release of Android Nougat also known as Android 7.0 (API 24) compileSdkVersion 24 is now available. To do that, you have to update your sdk. Step 1

Second, is to update your gradle files (internet connection required). Step 2

Also Update this part of app.gradle

Third (optional) Clean Project, Build Project (Both under Build Tab)

I hope this helped someone out there and save some few hours of researching.

Cheers / Happy coding



回答3:

i found two solutions,

first one is

app:statusBarScrim="@null"

second is to force app

compile ('com.android.support:support-v4:23.4.0') {
    force = true;
}

Sources taken from here



回答4:

I had the same issue, @Shumin's code is effective too but it is better to remove conflicts instead of bypassing them. To find the conflict -

  1. In Android Studio, open Terminal and enter gradlew app:dependencies.

  2. Check the dependencies tree and see which library is causing conflict

Update:- To check for conflicts, check the dependencies of each library and see if you aren't using the same dependency in some other library but with different version. For example, in the image below, com.android.support:appcompat-v7 is dependent on com.android.support:support-vector-drawable:23.4.0. If you find some other library depending on com.android.support:support-vector-drawable but the version is different, then you have found your conflict.



回答5:

My issue was that not all of the support library gradle imports we the same, eg, I had:

  compile('com.android.support:recyclerview-v7:23.4.0') {
        exclude module: 'support-v4'
    }
  compile 'com.android.support:support-annotations:24.0.0'

changing it so all imports were 24.0.0 enabled the application to run without issues.



回答6:

Hello Friend i face this issue

Just Make

compileSdkVersion 25
buildToolsVersion "25.0.2"

And All support gradle change to 25.1.1 it's work for me .



回答7:

please add below code in menifiest.xml.

 <activity android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and style resource file

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>


回答8:

I had the same issue showing the same error while inflating the CollapsingToolbar.. I had the following dependencies in my app gradle file

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.google.firebase:firebase-ads:10.0.1'

and build tool 25.0.2 with compiled with API 23 sdk. I than checked the dependencies and found that firebase uses

android:support:support-v4:24.0.0

and the appcompat uses

android:support:support-v4-23.4.0 -> 24.0

Hence, I changed all the build library number to

compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.google.firebase:firebase-ads:10.0.1'

It worked for me now. How durable ? I don't have any idea.



回答9:

This worked for me. Change you your appcompact and design libray version as follows,

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'