Android左侧导航栏点击后Fragment无法显示问题求助
Hey David, let's dig into why your Fragments aren't showing up when you tap the navigation drawer items. I spotted a couple of key issues in your code and layouts—let's fix them one by one.
1. Missing FrameLayout container in your layout
Looking at your content_main.xml and related layout files, there's no FrameLayout with the id frameLayout that you're trying to replace in your Fragment transaction:
fragmentManager.beginTransaction().replace(R.id.frameLayout, myFragment)...
Without this container, the Fragment has nowhere to render. You need to add it to your content_main.xml (or the appropriate parent layout). Update your content_main.xml 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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/viewBg" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="info.androidhive.kopilim.MainActivity" tools:showIn="@layout/activity_main"> <!-- Add this FrameLayout as the Fragment container --> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:scrollbars="vertical" /> </RelativeLayout>
Note: If you want the RecyclerView to only show on the home screen and be replaced by Fragments, consider moving the RecyclerView into a dedicated home Fragment instead of keeping it directly in content_main.
2. Uninitialized fragment variable in most cases
In your selectedItemDrawer method, only the manageProfile case sets the fragment variable to a valid Fragment class. For other cases like checkIn, checkOut, etc., fragment remains null. When you try to call fragment.newInstance(), this throws a NullPointerException (which gets caught, but leaves myFragment as null, so nothing renders).
Update each case to initialize its corresponding Fragment class:
switch(menuItem.getItemId()){ case R.id.checkIn: Toast.makeText(getApplicationContext(), "Check-In", Toast.LENGTH_SHORT).show(); fragment = fragment_checkIn.class; // Add this line break; case R.id.checkOut: Toast.makeText(getApplicationContext(), "Check-Out", Toast.LENGTH_SHORT).show(); fragment = fragment_checkOut.class; // Add this line break; case R.id.applyOff: fragment = fragment_applyOff.class; // Add this line break; case R.id.reportBug: fragment = fragment_reportBug.class; // Add this line break; case R.id.manageProfile: fragment = fragment_manageProfile.class; break; case R.id.logout: // Handle logout logic here (no Fragment needed) Toast.makeText(getApplicationContext(), "Logging out...", Toast.LENGTH_SHORT).show(); return; // Exit early since we don't need to replace a Fragment default: break; }
3. Guard against null myFragment before transaction
Even after fixing the above, if newInstance() throws an exception, myFragment will be null. Add a check to avoid trying to replace with a null Fragment:
try{ myFragment = (Fragment)fragment.newInstance(); }catch(Exception e){ e.printStackTrace(); Toast.makeText(this, "Failed to load Fragment", Toast.LENGTH_SHORT).show(); return; // Exit if Fragment creation fails } // Only proceed if myFragment is valid if(myFragment != null){ FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frameLayout, myFragment) .addToBackStack(null) .commit(); }
4. Verify setupDrawerContent connects to your handler
Make sure your setupDrawerContent method is actually triggering selectedItemDrawer when an item is tapped. If your MainActivity implements NavigationView.OnNavigationItemSelectedListener, confirm its listener method calls your handler:
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { selectedItemDrawer(item); return true; }
Or if you're setting up the listener inside setupDrawerContent, it should look like this:
private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(item -> { selectedItemDrawer(item); return true; }); }
Once you apply these fixes, your Fragments should render correctly when you tap the navigation drawer items.
内容的提问来源于stack exchange,提问作者David Adam

