com.google.android.material.navigation.NavigationView 无法投射到 androidx.drawerlayout.widget.DrawerLayout(Android Studio)
com.google.android.material.navigation.NavigationView cannot be cast to androidx.drawerlayout.widget.DrawerLayout (Android Studio)
我在启动应用程序然后点击菜单项后收到此错误(大多数菜单项都会导致此错误)。菜单看起来也不像以前那样 - 它应该是应用程序左侧的可折叠菜单,但它占据了整个屏幕并且不可折叠。我已将代码迁移到 AndroidX,并且我正在使用 Android Studio。我正在 API 28 上测试该应用程序,但此错误也会出现在 29 和 30 上。在模拟器和物理设备上。
如果我遗漏了本应添加到此 post 的代码,请告诉我 - 我是 Android Studio 的新手。谢谢你能给我的任何信息。我也遵循了这段代码here,它给了我同样的错误:
DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
这是错误日志:
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.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.onClick(NavigationMenuPresenter.java:416)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(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
这是 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;
}
来自 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());
来自 MenuBuilder 的代码:
boolean dispatchMenuItemSelected(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
return mCallback != null && mCallback.onMenuItemSelected(menu, item);
}
我查看了这个 post 并尝试了以下更改,但没有效果。
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
我找到了解决方案 - 我有两个 'activity_home.xml' 文件(一个用于两个不同的应用程序主题;另一个名为 'actiivty_home_red.xml')。在将应用程序迁移到 AndroidX 时,我有很多东西必须手动切换。在这两个文件中,我都将 com.google.android.material.navigation.NavigationView
作为外部标记和内部标记。我需要将外部标签更改为 androidx.drawerlayout.widget.DrawerLayout
。我不必更改任何其他文件中的任何内容。这 site 帮助指导了我正确的方向。
旧代码:
<?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代码:
<?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>
我在启动应用程序然后点击菜单项后收到此错误(大多数菜单项都会导致此错误)。菜单看起来也不像以前那样 - 它应该是应用程序左侧的可折叠菜单,但它占据了整个屏幕并且不可折叠。我已将代码迁移到 AndroidX,并且我正在使用 Android Studio。我正在 API 28 上测试该应用程序,但此错误也会出现在 29 和 30 上。在模拟器和物理设备上。
如果我遗漏了本应添加到此 post 的代码,请告诉我 - 我是 Android Studio 的新手。谢谢你能给我的任何信息。我也遵循了这段代码here,它给了我同样的错误:
DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
这是错误日志:
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.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.onClick(NavigationMenuPresenter.java:416)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access00(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
这是 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;
}
来自 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());
来自 MenuBuilder 的代码:
boolean dispatchMenuItemSelected(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
return mCallback != null && mCallback.onMenuItemSelected(menu, item);
}
我查看了这个 post 并尝试了以下更改,但没有效果。
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
我找到了解决方案 - 我有两个 'activity_home.xml' 文件(一个用于两个不同的应用程序主题;另一个名为 'actiivty_home_red.xml')。在将应用程序迁移到 AndroidX 时,我有很多东西必须手动切换。在这两个文件中,我都将 com.google.android.material.navigation.NavigationView
作为外部标记和内部标记。我需要将外部标签更改为 androidx.drawerlayout.widget.DrawerLayout
。我不必更改任何其他文件中的任何内容。这 site 帮助指导了我正确的方向。
旧代码:
<?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代码:
<?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>