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

Android Studio:点击BottomNavigationBar菜单项启动新Activity

Hey there! I see where you're going wrong here—you're using the wrong method to handle clicks for your BottomNavigationView. Let me walk you through fixing this step by step, since you're new to Android Studio.

The Core Issue

You’re using onOptionsItemSelected()—this method is meant for top ActionBar menu clicks, not the bottom navigation bar. Since you’ve already hidden the ActionBar with getSupportActionBar().hide(), those menu-related methods are completely unused and won’t trigger when you tap bottom items.

Step 1: Fix Your MainActivity.java Code

Remove the onCreateOptionsMenu and onOptionsItemSelected methods entirely, and replace them with a click listener specifically for BottomNavigationView. Here’s the corrected code:

package com.pkg.webview.webview;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide(); // Keep this to hide the top navigation bar
        setContentView(R.layout.activity_main);

        // Get a reference to your BottomNavigationView
        BottomNavigationView bottomNav = findViewById(R.id.navigation);
        
        // Set up the click listener for bottom menu items
        bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_item1:
                        startActivity(new Intent(MainActivity.this, secondactivity.class));
                        return true; // Marks the item as selected/highlighted
                    case R.id.action_item2:
                        startActivity(new Intent(MainActivity.this, third_activity.class));
                        return true;
                    case R.id.action_item3:
                        startActivity(new Intent(MainActivity.this, fourth_activity.class));
                        return true;
                }
                return false;
            }
        });
    }
}

Step 2: Double-Check Activity Registrations

Make sure all three target activities are registered in your AndroidManifest.xml—this is a common oversight for new developers. Add these lines inside the <application> tag:

<activity android:name=".secondactivity" />
<activity android:name=".third_activity" />
<activity android:name=".fourth_activity" />

Step 3: Confirm Your Bottom Nav Setup

Your activity_main.xml and navigation.xml are already set up correctly—you don’t need to change anything there. The BottomNavigationView is properly linked to your menu file, so the listener will pick up the item IDs correctly.

Quick Tips for Avoiding Future Issues

  • Follow Java naming conventions: Activity classes should start with a capital letter (e.g., SecondActivity instead of secondactivity). This helps avoid typos and makes your code easier to read.
  • Test incrementally: After making changes, run your app to check if each bottom item launches the correct activity before moving on.

内容的提问来源于stack exchange,提问作者M Waseem Sajjad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:28:38