NavigationView项跳转Activity遇问题,咨询XML作用及与Fragment区别
Hey there! Let's tackle your questions one by one, and also fix the core issue in your code first.
问题解答与代码修复
一、先解决你代码里的关键错误
Looking at your XML and Java code, I spotted a critical mistake:
- In your XML, you're using
BottomNavigationView, but in Java you're trying to cast it toNavigationView—these are two completely different controls! This will definitely throw aClassCastExceptionand break your code.
Corrected Code Examples:
XML (keep your structure, just ensure control type matches)
<android.support.v4.widget.DrawerLayout android:id="@+id/draw" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.BottomNavigationView android:id="@+id/nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="110dp" android:background="#ab9797" app:menu="@menu/men"> </android.support.design.widget.BottomNavigationView> </android.support.v4.widget.DrawerLayout>
Java (fix control type and set up the correct listener)
public class MainActivity extends AppCompatActivity { TextView textView1; BottomNavigationView bottomNavigationView; // Use the correct control type DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { bottomNavigationView = findViewById(R.id.nav); bottomNavigationView.setOnItemSelectedListener(new BottomNavigationView.OnItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); if (id == R.id.shark) { Intent newActivity00 = new Intent(MainActivity.this, Main18Activity.class); startActivity(newActivity00); // Close the drawer if needed drawerLayout = findViewById(R.id.draw); drawerLayout.closeDrawer(GravityCompat.START); } return true; // Return true to mark the item as selected } }); } catch (Exception e) { e.printStackTrace(); // Catch exceptions to debug easily } } }
二、Your Core Questions Answered
1. Do I have to use XML configuration to jump to another Activity when clicking menu items?
Absolutely not! XML is just a convenient way to define the structure of your menu (like item text, icons, and IDs). The jump logic is always handled in your Java/Kotlin code via click listeners, just like you tried to do. You could even create menu items entirely in code without XML, but XML is the standard approach because it's more readable and maintainable.
2. What's the difference between NavigationView and Fragment?
These are not comparable—they serve entirely different purposes:
- NavigationView: A Material Design UI control, mainly used to display a menu list in a side drawer (or other layouts). Its core job is to provide clickable menu items that trigger actions (like opening an Activity or switching a Fragment).
- Fragment: A core Android component, a modular UI fragment that can be embedded in an Activity. It has its own lifecycle and manages a portion of the UI and logic independently. In short, a Fragment is the "page content", while NavigationView is the "menu control that triggers content switches"—you can use NavigationView clicks to swap Fragments or launch new Activities.
内容的提问来源于stack exchange,提问作者Mrwan Yassin ALGANAD
相关产品推荐
相关产品推荐

