底部导航Switch Case中ID报错问题求助
解决底部导航Switch Case中ID报错的问题
核心问题:拼写错误
你代码里的R.id.actiom_home存在拼写错误——正确的单词应该是action,你写成了actiom(最后一个字母应为n而非m),这就导致代码中的ID和menu.xml里的正确ID无法匹配,直接引发报错。
修复步骤
修正代码中的拼写错误
将所有R.id.actiom_home替换为R.id.action_home,包括setSelectedItemId调用和switch case中的条目。修正后的代码片段如下:// 设置默认选中项 bottomNavigationView.setSelectedItemId(R.id.action_home); // ... 省略其他代码 ... switch (item.getItemId()){ case R.id.action_dashboard: Log.d("SwitchCase", "Dashboard selected"); startActivity(new Intent(getApplicationContext(), Dashboard.class)); overridePendingTransition(0,0); return true; case R.id.action_home: Log.d("SwitchCase", "Home selected"); return true; case R.id.about: Log.d("SwitchCase", "About selected"); startActivity(new Intent(getApplicationContext(), About.class)); overridePendingTransition(0,0); return true; default: Log.d("SwitchCase", "Default case"); return false; }同步检查menu.xml中的ID
确认menu.xml中对应的首页条目ID确实是action_home,示例如下:<item android:id="@+id/action_home" android:icon="@drawable/ic_home" android:title="Home" />清理并重新构建项目
在Android Studio中执行Build > Clean Project,再执行Build > Rebuild Project,确保资源ID重新生成,避免缓存导致的异常。
额外排查点
如果修正拼写后仍报错,可检查以下内容:
- 确认activity_main.xml中,BottomNavigationView的
app:menu属性已正确关联目标menu文件 - 检查是否存在重复的资源ID(比如其他布局或menu文件使用了相同ID)
- 确认Dashboard、About等关联Activity已在AndroidManifest.xml中注册
内容的提问来源于stack exchange,提问作者Khaled Al Shaar
相关产品推荐
相关产品推荐

