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

Android开发:如何通过BottomNavigationView的Trade选项打开Activity

Hey there! Let's get that BottomNavigationView working to launch your Trade Activity—here's a step-by-step breakdown that should fix things for you:

Step 1: Verify Your Trade Activity is Registered in AndroidManifest.xml

First things first, make sure your TradeActivity is properly declared in your AndroidManifest.xml—this is a super common oversight that causes crashes or silent failures:

<activity
    android:name=".TradeActivity"
    android:exported="false" /> 
<!-- Set `exported="true"` only if you need to launch this activity from outside your app -->
Step 2: Set Up the BottomNavigationView Listener

Next, in the Activity that hosts your BottomNavigationView, attach an item selection listener to trigger the Activity launch when "Trade" is tapped.

First, confirm your menu resource (res/menu/bottom_nav_menu.xml) has the Trade item:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home"
        android:title="Home"
        android:icon="@drawable/ic_home" />
    <item
        android:id="@+id/nav_trade" <!-- This ID must match what you use in the listener -->
        android:title="Trade"
        android:icon="@drawable/ic_trade" />
    <!-- Add other menu items here -->
</menu>

Then add the listener in your host Activity:

Kotlin Example:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
    bottomNav.setOnItemSelectedListener { menuItem ->
        when (menuItem.itemId) {
            R.id.nav_trade -> {
                // Launch TradeActivity
                val intent = Intent(this, TradeActivity::class.java)
                startActivity(intent)
                true // Return true to indicate the click was handled
            }
            R.id.nav_home -> {
                // Handle home tap (e.g., load a fragment)
                true
            }
            // Add cases for other menu items
            else -> false
        }
    }
}

Java Example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
    bottomNav.setOnItemSelectedListener(item -> {
        int itemId = item.getItemId();
        if (itemId == R.id.nav_trade) {
            Intent intent = new Intent(this, TradeActivity.class);
            startActivity(intent);
            return true;
        } else if (itemId == R.id.nav_home) {
            // Handle home tap logic
            return true;
        }
        return false;
    });
}
Common Troubleshooting Tips
  • Mismatched IDs: Double-check that the android:id for your Trade menu item exactly matches the ID you reference in the listener (case-sensitive!).
  • Old Listener Method: Avoid using the deprecated setOnNavigationItemSelectedListener—stick with setOnItemSelectedListener for better compatibility with newer AndroidX libraries.
  • Context Issues: Always use the current Activity instance (this) when creating the Intent, not getApplicationContext()—this avoids unexpected behavior with launch modes.

内容的提问来源于stack exchange,提问作者Mustafa Lokhandwala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:07:59