Search code examples
kotlinbottomnavigationviewandroid-viewbinding

Bottom Navigation View Throw android.view.InflateException When I run the app in my Phone


what I know so far is that the THEME have a problem in night mode and it works perfectly fine in normal THEME

so I just created a bottom navigation in android studio and when I run the app on emulator it works fine but when I run it on my phone (Xiaomi Redmi Note 9 Pro) it will crash after my splash activity

I have used the android studio DEAFULT Bottom Navigation Activity

when I created another project with default Nav activity it worked fine

Here is the log


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.foroshgah_slami, PID: 15695
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foroshgah_slami/com.example.foroshgah_slami.activities.DashboardActivity}: android.view.InflateException: Binary XML file line #19 in com.example.foroshgah_slami:layout/activity_dashboard: Binary XML file line #19 in com.example.foroshgah_slami:layout/activity_dashboard: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3771)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
     Caused by: android.view.InflateException: Binary XML file line #19 in com.example.foroshgah_slami:layout/activity_dashboard: Binary XML file line #19 in com.example.foroshgah_slami:layout/activity_dashboard: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
     Caused by: android.view.InflateException: Binary XML file line #19 in com.example.foroshgah_slami:layout/activity_dashboard: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:881)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1033)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:988)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1150)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1111)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:709)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:547)
        at com.example.foroshgah_slami.databinding.ActivityDashboardBinding.inflate(ActivityDashboardBinding.java:49)
        at com.example.foroshgah_slami.databinding.ActivityDashboardBinding.inflate(ActivityDashboardBinding.java:43)
        at com.example.foroshgah_slami.activities.DashboardActivity.onCreate(DashboardActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:8163)
        at android.app.Activity.performCreate(Activity.java:8130)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1330)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3744)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2291)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:244)
        at android.content.res.MiuiResourcesImpl.getValue(MiuiResourcesImpl.java:96)
        at android.content.res.Resources.getValue(Resources.java:1457)
        at androidx.core.content.res.ResourcesCompat.isColorInt(ResourcesCompat.java:309)
        at androidx.core.content.res.ResourcesCompat.inflateColorStateList(ResourcesCompat.java:256)
        at androidx.core.content.res.ResourcesCompat.getColorStateList(ResourcesCompat.java:236)
        at androidx.core.content.ContextCompat.getColorStateList(ContextCompat.java:558)
        at androidx.appcompat.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:48)
        at com.google.android.material.navigation.NavigationBarMenuView.createDefaultColorStateList(NavigationBarMenuView.java:660)
        at com.google.android.material.navigation.NavigationBarMenuView.<init>(NavigationBarMenuView.java:113)
        at com.google.android.material.bottomnavigation.BottomNavigationMenuView.<init>(BottomNavigationMenuView.java:50)
        at com.google.android.material.bottomnavigation.BottomNavigationView.createNavigationBarMenuView(BottomNavigationView.java:216)
        at com.google.android.material.navigation.NavigationBarView.<init>(NavigationBarView.java:158)
        at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:108)
        at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:103)
        at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:98)
            ... 28 more


Here is the dashboardActivity


package com.example.foroshgah_slami.activities

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.foroshgah_slami.R
import com.example.foroshgah_slami.databinding.ActivityDashboardBinding

class DashboardActivity : BaseActivity() {

    private lateinit var binding: ActivityDashboardBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityDashboardBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_dashboard)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onBackPressed() {
        doubleBackToExitPressedOnce()
    }
}


The XML file of Activity


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_dashboard"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>


and this is what's happening in my splash activity



package com.example.foroshgah_slami.activities

import android.content.Intent
import android.os.Build
import android.os.Build.VERSION_CODES
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.WindowInsets
import android.view.WindowManager
import com.example.foroshgah_slami.R

class splash_activity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        if (Build.VERSION.SDK_INT >= VERSION_CODES.R){
            window.insetsController?.hide(WindowInsets.Type.statusBars())
        } else {
            window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
        }
        Handler().postDelayed({
            startActivity(Intent(this@splash_activity, DashboardActivity::class.java))
            finish()
        } ,2500)
    }
}



I having View binding in all of my code and it works perfectly fine on my Phone


Solution

  • the problem was that I had 2 Extra style

    <item name="android:textColorPrimary">@color/white</item>
        <item name="android:textColorSecondary">#C1C1C1</item>
    

    in night mode and when I ran the app it crashed

    so if you have any problem that points to XML check every style and value that you gave