Home > Back-end >  Error inflating class on BottomNavigationView and style on this component requires your app theme to
Error inflating class on BottomNavigationView and style on this component requires your app theme to

Time:01-12

I'm getting two errors, which probably relate to the BottomNavigationView in my DashBoardActivity. This only occurs for some users and not all.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adit.bangkit.plagroid/com.adit.bangkit.plagroid.ui.activities.DashboardActivity}: android.view.InflateException: Binary XML file line #8 in com.adit.bangkit.plagroid:layout/activity_dashboard: Binary XML file line #8 in com.adit.bangkit.plagroid:layout/activity_dashboard: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
    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:2210)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7839)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
 Caused by: android.view.InflateException: Binary XML file line #8 in com.adit.bangkit.plagroid:layout/activity_dashboard: Binary XML file line #8 in com.adit.bangkit.plagroid:layout/activity_dashboard: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
 Caused by: android.view.InflateException: Binary XML file line #8 in com.adit.bangkit.plagroid: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:858)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
    at com.adit.bangkit.plagroid.databinding.ActivityDashboardBinding.inflate(ActivityDashboardBinding.java:49)
    at com.adit.bangkit.plagroid.databinding.ActivityDashboardBinding.inflate(ActivityDashboardBinding.java:43)
    at com.adit.bangkit.plagroid.ui.activities.DashboardActivity.onCreate(DashboardActivity.kt:21)
    at android.app.Activity.performCreate(Activity.java:8051)
    at android.app.Activity.performCreate(Activity.java:8031)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
    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:2210)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7839)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
        at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:247)
        at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:212)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:147)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:114)
        at com.google.android.material.navigation.NavigationBarView.<init>(NavigationBarView.java:145)
        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)

here is my dashboard activity:

class DashboardActivity : BaseActivity() {
private lateinit var binding: ActivityDashboardBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityDashboardBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Update the background color of the action bar as per our design requirement.
    supportActionBar!!.setBackgroundDrawable(
        ContextCompat.getDrawable(
            this@DashboardActivity,
            R.drawable.app_gradient_color_background
        )
    )
    // END

    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    val navController = findNavController(R.id.nav_host_fragment)
    // 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_dashboard,
            R.id.navigation_orders,
            R.id.navigation_sold_products
        )
    )
    setupActionBarWithNavController(navController, appBarConfiguration)

    navView.setupWithNavController(navController)
}

override fun onBackPressed() {
    doubleBackToExit()
}
}

Here is my BottomNavigationView details:

<?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">

<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="@drawable/app_gradient_color_background"
    app:itemIconTint="@color/colorWhite"
    app:itemTextColor="@color/colorWhite"
    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"
    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>

Line 8 in the error points to <com.google.android.material.bottomnavigation.BottomNavigationView. This is my layout for bottomNavigationView.

My application theme inherits from "Theme.MaterialComponents.Light.NoActionBar.Bridge".

Firstly, my bottom_nav_colours was in the drawable folder and it crashes for some users. Tried to duplicate it also in the existing drawable-any-dpi-v24 folder and it still crashes.

Not sure what to do here. I think the issue is to do with <com.google.android.material.bottomnavigation.BottomNavigationView in the activity_dashboard.

i've tried to update di library and re-checking all resources that used in layout but can't work. hope with this question it can be solved

CodePudding user response:

I had this issue too.. it seems like to be a bug.. Just change in xml:

<fragment
       .....

To

<androidx.fragment.app.FragmentContainerView
       .....

and in code instead of:

val navController = findNavController(R.id.nav_host_fragment)

to

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
  • Related