Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView

Saman Sattari picture Saman Sattari · Oct 26, 2018 · Viewed 13.7k times · Source

I'm trying to add BottomNavigationView to my layout with this library:

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

This is my layout:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".home.HomeActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        app:itemBackground="?colorPrimary"
        app:itemIconTint="@drawable/bottom_navigation_selector"
        app:itemTextColor="@drawable/bottom_navigation_selector"
        app:layout_insetEdge="bottom"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:menu="@menu/bottom_navigation" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:fabSize="normal"
        app:layout_anchor="@id/bottom_navigation"
        app:layout_anchorGravity="end"
        app:srcCompat="@drawable/ic_add_white_24dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

It runs on my emulator but when I want to launch app on my real device it gives me this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rightechco.rayanpairpanel/com.rightechco.rayanpairpanel.home.HomeActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2489)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551)
        at android.app.ActivityThread.access$1000(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5572)
        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:968)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
     Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
        at android.view.LayoutInflater.createView(LayoutInflater.java:637)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23)
        at android.app.Activity.performCreate(Activity.java:6033)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551) 
        at android.app.ActivityThread.access$1000(ActivityThread.java:169) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        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:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:611)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6033) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551) 
        at android.app.ActivityThread.access$1000(ActivityThread.java:169) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        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:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f07007b
        at android.content.res.Resources.getValue(Resources.java:1446)
        at android.content.res.MiuiResources.getValue(MiuiResources.java:146)
        at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
        at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
        at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
        at androidx.appcompat.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:505)
        at com.google.android.material.bottomnavigation.BottomNavigationItemView.initialize(BottomNavigationItemView.java:105)
        at com.google.android.material.bottomnavigation.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:523)
        at com.google.android.material.bottomnavigation.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
        at com.google.android.material.bottomnavigation.BottomNavigationView.inflateMenu(BottomNavigationView.java:254)
        at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:187)
        at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:114)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:611) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6033) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551) 
        at android.app.ActivityThread.access$1000(ActivityThread.java:169) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5572) 
        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:968) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763) 

EDIT: And this is my activity code, if helps any:

class HomeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        setSupportActionBar(toolbar)

        handleBottomNavigationView()

    }

private fun handleBottomNavigationView() {
        bottom_navigation.setOnNavigationItemSelectedListener {
            val id = it.itemId
            when (id) {
                R.id.bottom_navigation_orders -> openOrderFragment()
                R.id.bottom_navigation_tariffs -> openTariffFragment()
                R.id.bottom_navigation_laptops -> openLaptopFragment()
                R.id.bottom_navigation_mobiles -> openMobileFragment()
                else -> false
            }
        }
}

Edit2: and this is all of my dependecies:

dependencies {
    // Project
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Support Libraries
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'

    // Animations
    implementation 'com.github.florent37:viewanimator:1.0.5'

    // Lifecycle Aware Components
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    // User Interface
    implementation 'com.github.yalantis:ucrop:2.2.2'
    implementation 'net.steamcrafted:load-toast:1.0.12'
    implementation 'com.afollestad.material-dialogs:core:2.0.0-beta4'
    implementation 'com.github.alxrm:audiowave-progressbar:0.9.2'
    implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'

    // Dependency Injection
    implementation 'com.google.dagger:dagger:2.16'
    kapt 'com.google.dagger:dagger-compiler:2.16'

    // Runtime Permissions
    implementation 'com.github.hotchemi:permissionsdispatcher:4.0.0-alpha1'
    kapt 'com.github.hotchemi:permissionsdispatcher-processor:4.0.0-alpha1'

    // Rest Services
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'

    // Image Processing
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'jp.wasabeef:picasso-transformations:2.2.1'

    // Debugging
    implementation 'com.jakewharton.timber:timber:4.7.1'

    // Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-beta02'
}

Answer

forpas picture forpas · Oct 26, 2018

Instead of

app:itemBackground="?colorPrimary"

set

app:itemBackground="@color/colorPrimary"

Edit: also check that the drawables are in res/drawable folder and not in a folder like res/drawable-v21