Error inflating class Android support design

13.5k views Asked by At

What I am trying to do is use the new android support design library mentioned here: http://android-developers.blogspot.com/2015/05/android-design-support-library.html.

I have attempted to use the floating action button as follows in my xml layout (this is where the below error is coming from):

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/adview"
    android:layout_marginTop="500dp" />

When my app is launched this is the error:

06-06 20:56:43.186    6405-6405/com.nick.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nick.app, PID: 6405
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nick.app/com.nick.app.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.design.widget.FloatingActionButton
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
        at android.app.ActivityThread.access$800(ActivityThread.java:149)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5290)
        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:908)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.design.widget.FloatingActionButton
        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.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:401)
        at android.app.Activity.setContentView(Activity.java:2197)
        at com.nick.app.MainActivity.onCreate(MainActivity.java:94)
        at android.app.Activity.performCreate(Activity.java:6020)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2366)
            at android.app.ActivityThread.access$800(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5290)
            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:908)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

in my build.gradle file for the app module I have compile 'com.android.support:design:22.2.0' as required.Also, other relevant parts from the build.gradle file:

compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
    ...
    minSdkVersion 16
    targetSdkVersion 21
    .....

I should also note that when I go to project structure->dependencies and I try to add a library dependency I cannot find "com.android.support:design:22.2.0" when searching (I am not sure if this is required in addition to adding the dependency in build.gradle file).

Finally, in my SDK manager I have confirmed "Android Support Repository" and "Android Support Library" are up to date at versions 15 and 22.2, respectively.

Are there any suggestions for what else I might try?

I have found this answer: https://stackoverflow.com/a/30557995/1489990 and tried the solution however it did not solve my issue.

5

There are 5 answers

2
ianhanniballake On

If you are using any of the Design Library components, you should ensure your activity extends AppCompatActivity and uses an appropriate Theme.AppCompat theme. Note that the FloatingActionButton relies on the colorAccent set on your theme - ensure your theme has that defined.

0
ssudaraka On

If everything else doesn't work, Upgrade the version of the dependency and version. In your build.gradle file:

compileSdkVersion 24

compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
0
Dharmendra Mishra On

I had the same error. After more Search i have got solution. Just Change build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24 // previous 23

    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "com.ventrux.thevideoapp"
        minSdkVersion 15
        targetSdkVersion 24  // previous 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    compile 'com.android.support:appcompat-v7:24.2.1'       // previous 23.2.1
    compile 'com.android.support:design:24.2.1'          // previous 23.2.1

    compile files('libs/YouTubeAndroidPlayerApi.jar')
}

Xml for FloatingActionButton

  <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@drawable/plus_iconaddvideo"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="30dp"
            />
0
ryanafrish7 On

I had the same issue. I resolved it build cleaning and rebuilding the project. When I ran it again, it had no such exceptions thrown.

0
Vishal Puri On

This error can be caused due to reasons as mentioned below.

  1. This problem will likely occur when the version of your appcompat library and design support library doesn't match.

Example of matching condition

  compile 'com.android.support:appcompat-v7:24.2.0' // appcompat library

  compile 'com.android.support:design:24.2.0' //design support library
  1. Your theme file in styles should not have

    <item name="colorPrimary">#4A0958</item>
    <item name="colorPrimaryDark">#4A0958</item>
    

    It should look somewhat like this.

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    
     <item name="colorPrimary">#4A0958</item>
     <item name="colorPrimaryDark">#4A0958</item>
     <item name="colorAccent">#4A0958</item>
    
    </style>