Search code examples
javaandroidandroid-studionavigation-drawerandroidx

com.google.android.material.navigation.NavigationView cannot be cast to androidx.drawerlayout.widget.DrawerLayout (Android Studio)


I am receiving this error after launching the app and then tapping a menu item (most of the menu items result in this error). The menu is also not looking like it did before - it is supposed to be a collapsible menu on the left side of the app, but it takes up the whole screen and isn't collapsible. I have migrated the code to AndroidX, and I'm using Android Studio. I am testing the app on API 28, but this error comes up on 29 and 30 as well. Both in the emulator and on a physical device.

Let me know if I am missing code that I should have added to this post - I am very new to Android Studio. Thank you for any information you can give me. I also followed this code here and it is giving me the same error:

DrawerLayout mDrawerLayout;
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }

This is the error log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ccgo, PID: 6729
    java.lang.ClassCastException: com.google.android.material.navigation.NavigationView cannot be cast to androidx.drawerlayout.widget.DrawerLayout
        at com.ccgo.HomeActivity.onNavigationItemSelected(HomeActivity.java:858)
        at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:217)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:416)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 6729 SIG: 9

Here's the code for HomeActivity.java:

public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
       switch (item.getItemId())
       {
           //******************Admin Navigation****************
           case R.id.nav_mng_home:
               mImgDashboard.setVisibility(View.VISIBLE);
               mTxtFragmentName.setVisibility(View.GONE);
               setBottomMenuIconAdmin(1);

               if(Integer.parseInt(pref.getRoleId())==AppConstant.UserType.ADMIN)
               {
                   setFragment(new FragmentAdminDashboard());
               }
               else if(Integer.parseInt(pref.getRoleId())==AppConstant.UserType.COACH)
               {
                   setFragment(new FragmentCoachDashboard());
               }
               else if(Integer.parseInt(pref.getRoleId())==AppConstant.UserType.USER)
               {
                   setFragment(new FragmentUserDashboard());
               }
               break;

           case R.id.nav_mng_challenge:
               setFragment(new FragmentManageChallenge());
               mImgDashboard.setVisibility(View.GONE);
               mTxtFragmentName.setVisibility(View.VISIBLE);
               mTxtFragmentName.setText(R.string.mng_challenges);
               setBottomMenuIconAdmin(2);
               break;

//I removed the cases & some other code for brevity

        DrawerLayout drawer = findViewById(R.id.drawer_layout); //This is line 858
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Code snippet from NavigationView.java:

 this.menu.setCallback(
        new MenuBuilder.Callback() {
          @Override
          public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
            return listener != null && listener.onNavigationItemSelected(item); //This is line 217
          }

          @Override
          public void onMenuModeChange(MenuBuilder menu) {}
        });
    presenter.setId(PRESENTER_NAVIGATION_VIEW_ID);
    presenter.initForMenu(context, this.menu);
    presenter.setItemIconTintList(itemIconTint);
    presenter.setOverScrollMode(getOverScrollMode());

Code from MenuBuilder:

boolean dispatchMenuItemSelected(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
    return mCallback != null && mCallback.onMenuItemSelected(menu, item);
}

I have looked at this post and tried the following change but it didn't make a difference.

DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

Solution

  • I found the solution - I have two 'activity_home.xml' files (one for two different app themes; the other is called 'actiivty_home_red.xml'). When migrating the app to AndroidX, I had a lot of stuff I had to manually switch over. In both of these files, I had com.google.android.material.navigation.NavigationView as the outer tag and the inner tag. I needed to change the outer tag to androidx.drawerlayout.widget.DrawerLayout. I didn't have to change anything in any other files. This site helped guide me in the right direction.

    Old code:

    <?xml version="1.0" encoding="utf-8"?>
    <com.google.android.material.navigation.NavigationView 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="false"
        tools:openDrawer="start">
    
        <include
            layout="@layout/app_bar_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:fitsSystemWindows="true"
            app:theme="@style/ThemeOverlay.AppCompat.navTheme"
            app:headerLayout="@layout/nav_header_main_red"
            app:menu="@menu/activity_main_drawer" />
    
    </com.google.android.material.navigation.NavigationView>
    

    Updated/working code:

    <?xml version="1.0" encoding="utf-8"?>
    <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="false"
        tools:openDrawer="start">
    
        <include
            layout="@layout/app_bar_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:fitsSystemWindows="true"
            app:theme="@style/ThemeOverlay.AppCompat.navTheme"
            app:headerLayout="@layout/nav_header_main_red"
            app:menu="@menu/activity_main_drawer" />
    
    </androidx.drawerlayout.widget.DrawerLayout>