Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error inflating ConstraintLayout in android studio

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">
like image 925
ausgeorge Avatar asked Feb 28 '17 06:02

ausgeorge


People also ask

What is the use of ConstraintLayout in Android?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

How do I change ConstraintLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.


4 Answers

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

like image 59
arjun Avatar answered Oct 20 '22 06:10

arjun


I don't know if this was solution 2 and half years ago, but today i had similar error. Found the answer right here: Error inflating class androidx.constraintlayout.ConstraintLayout after migration to androidx

Basically only xml tags of constraint layout should be renamed to these ->

androidx.constraintlayout.widget.ConstraintLayout

like image 35
ZeePee Avatar answered Oct 20 '22 05:10

ZeePee


If You using androidx, you must use

androidx.constraintlayout.widget.ConstraintLayout
like image 4
Izzamed Avatar answered Oct 20 '22 06:10

Izzamed


It may be number of reasons

  1. dependency version issue (same like above)

  2. out-of-memory issue : In my case I was added the high resolution image for background of the constraint-layout or in the same page(.xml file) we used the high resolution image for any imageview source.

like image 2
Kona Suresh Avatar answered Oct 20 '22 05:10

Kona Suresh