'I'm getting a "Error inflating class com.google.android.material.navigation.NavigationView"
I have looked at other posts to this problem and I can't seem to solve it, so I'm hoping that its just a case that I am missing something obvious.
Here is my code: MainActivity.XML
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/translation_panel_blue_open_mea"
android:minHeight="57dp"
android:padding="1dp"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextColor="@color/header_grey_mea"
app:titleTextColor="@color/header_grey_mea"
android:theme="@style/AppTheme">
</androidx.appcompat.widget.Toolbar>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme"
app:menu="@menu/burger_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
burger_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/menuLogout"
android:onClick="initUserLogout"
android:title="@string/log_out">
</item>
<item
android:id="@+id/menuItemTutorial"
android:onClick="launchTutorialView"
android:title="@string/tutorial">
</item>
</group>
</menu>
AppTheme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and my initBurgerMenu
function that runs in MainActivity
:
private fun initBurgerMenu(){
mDrawerLayout = findViewById(R.id.drawer_layout)
val navigationView: NavigationView = findViewById(R.id.nav_view)
navigationView.setNavigationItemSelectedListener { menuItem ->
// set item as selected to persist highlight
menuItem.isChecked = true
// close drawer when item is tapped
mDrawerLayout.closeDrawers()
when (menuItem.itemId) {
R.id.menuLogout -> {
initUserLogout()
}
R.id.menuItemTutorial -> {
Toast.makeText(this, "tutorial", Toast.LENGTH_LONG).show()
}
}
true
}
}
Solution 1:[1]
Close your AppBarLayout tag, like this
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="57dp"
android:padding="1dp"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/AppTheme">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
Solution 2:[2]
Your navigation view in the xml is from com.google.android.material.navigation.NavigationView
and you're probably trying to inflate NavigationView
from android.support.design.widget
, if this is the problem just delete the import and reimport the correct package.
Solution 3:[3]
I had the same issue:
This happened after updating various dependencies to latest.
I updated the com.google.android.material:material
from 1.4.0-beta01
to 1.7.0-alpha01
.
After reverting the version to 1.4.0-beta01
the problem resolved.
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 | Shubham Raitka |
Solution 2 | Tim |
Solution 3 | Andreas Foteas |