error inflating ConstraintLayout in android studio

ausgeorge picture ausgeorge · Feb 28, 2017 · Viewed 44.4k times · Source

I'm having problems with Android Studio. I recently upgraded my SDK Manager to include;

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'

classpath is

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

Project builds fine. Intellisense and documentation works fine. But when I try to run (in debug) the app on my phone I get this error;

 Caused by: android.view.InflateException: Binary XML file line #2:
 Binary XML file line #2: Error inflating class ConstraintLayout
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                   at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:443)
                   at android.app.Activity.setContentView(Activity.java:2172)

I've synced gradle. I've restarted Android Studio. I've cleaned and rebuilt the project.

Any ideas on how to fix this? thanks!

-edit- XML looks like this;

<ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/PLAY_PARENT"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenfelt2"
android:padding="0dp">

Answer

arjun picture arjun · Feb 28, 2017

You should add as below

<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/PLAY_PARENT"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/greenfelt2"
 android:padding="0dp">

compile 'com.android.support.constraint:constraint-layout:1.0.2'

For the latest release check here

check out this project for more on ConstraintLayout Usage

Update

For those who are switching from android support to androidx, remember to change android.support.constraint.ConstraintLayout to androidx.constraintlayout.widget.ConstraintLayout