'Error inflating class androidx.appcompat.widget.Toolbar android

I try to use the toolbar in my app and I get this message: "Error inflating class androidx.appcompat.widget.Toolbar". i glad if someone will find my problem (using the latest version of the android studio)

XML:

  <androidx.appcompat.widget.Toolbar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#00008577"
        android:minHeight="?actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/main"
        android:id="@+id/toolbar"/>

JAVA:


    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        setSupportActionBar(toolbar);

    }

    private void findViews() {
        drawer_layout = findViewById(R.id.drawer_layout);
        nav_view = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);
    }

STYLE: <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

Exception received

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.or.ourshoppinglist/com.or.ourshoppinglist.MainActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class <unknown>


Solution 1:[1]

Hi noa shen and welcome to Stackoverflow.

Firstly, there can be an issue with your import for Toolbar. Since androix version came out now you got two versions of Toolbar:

androidx.appcompat.widget.Toolbar

and

android.support.v7.widget.Toolbar

So first check your imports in your JAVA.class and see if you imported the right version which would be:

androidx.appcompat.widget.Toolbar

Besides that, why is your toolbar width equals to 0?

<androidx.appcompat.widget.Toolbar
    android:layout_width="0dp"

Also to use toolbar you need to paste this in your build Gradle file in dependencies:

implementation 'androidx.appcompat:appcompat:1.0.0'

If this doesn't solve anything please provide your whole XML, JAVA.class, and full stack trace of your exception.

Solution 2:[2]

added my whole XML

<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        app:headerLayout="@layout/header"
        app:menu="@menu/main_menu" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="#00008577"
        android:minHeight="?actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/main"
        android:id="@+id/toolbar"/>


</androidx.constraintlayout.widget.ConstraintLayout>


</ScrollView>


</androidx.drawerlayout.widget.DrawerLayout>

JAVA:

package com.or.ourshoppinglist;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;

import com.google.android.material.navigation.NavigationView;


public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawer_layout;
    private NavigationView nav_view;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        setSupportActionBar(toolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer_layout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer_layout.addDrawerListener(toggle);
        toggle.syncState();
    }

    private void findViews() {
        drawer_layout = findViewById(R.id.drawer_layout);
        nav_view = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);
    }
}

Solution 3:[3]

In build.gradle (module app) file, implementation of The upgraded version : implementation 'androidx.appcompat:appcompat:1.4.1' caused me the error. When I used implementation 'androidx.appcompat:appcompat:1.0.0' , The error has gone.

Solution 4:[4]

I fixed this by changing android:background to app:srcCompat when I am using ?android:attr.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SlothCoding
Solution 2 noa shen
Solution 3 Noor Hossain
Solution 4 Zero