You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 to NavigationView—these are two completely different controls! This will definitely throw a ClassCastException and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:53:50