10

I added a toolbar to my layout and now I get this error when running:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/mainactivity.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #2: Error inflating class android.support.design.widget.AppBarLayout

This is my activity layout:

<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">

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

</androidx.constraintlayout.widget.ConstraintLayout>

and this is my appbar layout:

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </android.support.v7.widget.Toolbar>

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

In my activity onCreate():

Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setTitle(getTitle());
        }

I can't figure out why. Any suggestions?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
CJR
  • 3,174
  • 6
  • 34
  • 78
  • try wrapping inside `CoordinatorLayout` instead of `ConstraintLayout` . also i see you are missing constraint for your embedded layout – Faisal Naseer Mar 22 '19 at 11:41
  • and also try replacing `androidx.constraintlayout.widget.ConstraintLayout` with `android.support.constraint.ConstraintLayout` – Faisal Naseer Mar 22 '19 at 11:42
  • and also check if you have complete setup for migration https://developer.android.com/jetpack/androidx/migrate – Faisal Naseer Mar 22 '19 at 11:43
  • If you are using Android Studio's `Migrate to AndroidX` wizard, check [this answer.](https://stackoverflow.com/a/54579196/2408879) – makata Dec 16 '19 at 21:33

3 Answers3

39

You need to use

<com.google.android.material.appbar.AppBarLayout
<androidx.appcompat.widget.Toolbar

Instead of

<android.support.design.widget.AppBarLayout
<android.support.v7.widget.Toolbar

SAMPLE CODE

<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

Make sure u have added below dependencies

implementation 'com.google.android.material:material:1.0.0'

UPDATE

if you have used Toolbar and AppBarLayout inside your activity then make sure you have correct imports inside your activity code

import androidx.appcompat.widget.Toolbar
import com.google.android.material.appbar.AppBarLayout
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • is it a MUST use com.google.android.material.appbar.AppBarLayout and androidx.appcompat.widget.Toolbar ? is it possible a solution support library? – metis Aug 01 '19 at 22:10
  • 1
    @metis if you are using `androidx` `dependencies` then you need to use ` – AskNilesh Aug 02 '19 at 04:32
7

@Nilesh answer worked for me but there is more. For those who use androidx;

change these in your layout;

android.support.design.widget.CoordinatorLayout
android.support.design.widget.AppBarLayout
android.support.v7.widget.Toolbar
android.support.design.widget.TabLayout
android.support.v4.view.ViewPager

to this;

androidx.coordinatorlayout.widget.CoordinatorLayout
com.google.android.material.appbar.AppBarLayout
androidx.appcompat.widget.Toolbar
com.google.android.material.tabs.TabLayout
androidx.viewpager.widget.ViewPager
Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
3

Adding further to @NileshRathod's answer, errors regarding inflating classes occur almost only due to incorrect dependencies in the app level build.gradle file.

You must add the following dependency in you app level build.gradle file:

implementation 'com.android.support:design:23.1.0'

Well, I suggest you to use Material or AndroidX dependencies

Gourav
  • 2,746
  • 5
  • 28
  • 45