Android新手引用BottomNavigationView布局致App崩溃求助
Hey there! Let's get to the bottom of why your app keeps crashing when you try to add that bottom navigation bar. As a fellow dev who's been there, I'll walk you through the most common issues and how to fix them.
First, Fix the Layout Structure
From your code snippet, it looks like you're trying to nest the BottomNavigationView inside a Toolbar's layout—and that's probably part of the problem. Toolbars are meant for top navigation, and cramming a bottom nav inside it can cause layout conflicts or rendering errors. Instead, place both components in a single parent layout, like this:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Top Toolbar --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <!-- Bottom Navigation Bar --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#FFFFFF" app:menu="@menu/your_bottom_menu" /> <!-- Replace with your menu XML filename --> </RelativeLayout>
Next, Check Dependencies & Library Migration
You used android.support.design.widget.BottomNavigationView in your code—but the old Support Library is deprecated now. Switch to the AndroidX Material Components library instead, which is actively maintained. Here's what to do:
- Open your module-level
build.gradlefile (not the project-level one) - Add or update these dependencies:
dependencies { implementation 'com.google.android.material:material:1.9.0' // Use the latest stable version implementation 'androidx.appcompat:appcompat:1.6.1' }
- Sync your project with Gradle files—this will fix any
ClassNotFoundExceptioncrashes from missing library classes.
Verify Your Menu XML
Make sure your bottom navigation menu file (in res/menu/) is properly formatted. A valid menu looks like this:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" app:showAsAction="ifRoom" /> <item android:id="@+id/nav_profile" android:icon="@drawable/ic_profile" android:title="Profile" app:showAsAction="ifRoom" /> <item android:id="@+id/nav_settings" android:icon="@drawable/ic_settings" android:title="Settings" app:showAsAction="ifRoom" /> </menu>
Double-check that all android:id values are unique, and that your icon drawables exist in the res/drawable/ folder.
Initialize Components in Your Activity
Finally, wire up the toolbar and bottom navigation in your activity to avoid null pointer exceptions:
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import com.google.android.material.bottomnavigation.BottomNavigationView; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set up the Toolbar Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Set up Bottom Navigation BottomNavigationView bottomNav = findViewById(R.id.bottom_nav); bottomNav.setOnNavigationItemSelectedListener(item -> { // Handle navigation clicks here switch (item.getItemId()) { case R.id.nav_home: // Navigate to Home screen return true; case R.id.nav_profile: // Navigate to Profile screen return true; case R.id.nav_settings: // Navigate to Settings screen return true; default: return false; } }); } }
Common Crash Culprits to Double-Check
- Deprecated Library: Using the old Support Library instead of AndroidX is a top cause of crashes here.
- Layout Nesting: Putting BottomNavigationView inside a Toolbar creates an invalid hierarchy.
- Missing Resources: If your menu references an icon that doesn't exist, the app will crash on launch.
- Null Pointers: Forgetting to initialize the toolbar or bottom nav in your activity can cause crashes when the layout inflates.
Give these steps a try, and your app should stop crashing while showing both the toolbar and bottom navigation bar properly!
内容的提问来源于stack exchange,提问作者Talha Khalid

