片段不调用 OnCreate 方法

Fragment don't call OnCreate method

我想使用 Fragments 制作底部导航布局。但是我无法在我的应用程序中显示片段。我已经展示了很多片段的视频,尝试了很多方法来展示片段,但我仍然无法在我的应用程序中展示片段。如果可能的话,请解决我的问题。

package com.example.login;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;

import com.example.login.ui.login.ChatFragment;
import com.example.login.ui.login.HomeFragment;
import com.example.login.ui.login.MyAddsFragment;
import com.example.login.ui.login.PostAddFragment;
import com.example.login.ui.login.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemReselectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_home2);
   BottomNavigationView navView = findViewById(R.id.nav_view);
   navView.setOnNavigationItemReselectedListener(this);
   }

   private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
               Fragment fragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        break;

                    case R.id.navigation_chat:
                        fragment = new ChatFragment();
                        break;

                    case R.id.navigation_post_add:
                        fragment = new PostAddFragment();
                        break;

                    case R.id.navigation_my_adds:
                        fragment = new MyAddsFragment();
                        break;

                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        break;


                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
                return true;
            }
        };

        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {

        }
    }

XML 文件 activity_home.xml

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">



<fragment
    android:id="@+id/fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/mobile_navigation"
    android:layout_marginBottom="56dp"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        app:itemIconTint="@color/orange"
        app:itemTextColor="@color/black"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu" />

</RelativeLayout>

您忘记设置监听器了。添加:

navView.setOnNavigationItemSelectedListener(navListener);

此外,根据您最近的修改,这是不允许的:

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();

因为fragment_container是一个片段。它应该是 ViewGroup 类型,例如 LinearLayout。

你忘了写这行:

 navView.setNavigationItemSelectedListener(this);

您的新更新代码是:

package com.example.login;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;

import com.example.login.ui.login.ChatFragment;
import com.example.login.ui.login.HomeFragment;
import com.example.login.ui.login.MyAddsFragment;
import com.example.login.ui.login.PostAddFragment;
import com.example.login.ui.login.ProfileFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemReselectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_home2);
   BottomNavigationView navView = findViewById(R.id.nav_view);
   navView.setOnNavigationItemSelectedListener(navListener);
   navView.setOnNavigationItemReselectedListener(navView);
   }

   private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
               Fragment fragment = null;
                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        fragment = new HomeFragment();
                        break;

                    case R.id.navigation_chat:
                        fragment = new ChatFragment();
                        break;

                    case R.id.navigation_post_add:
                        fragment = new PostAddFragment();
                        break;

                    case R.id.navigation_my_adds:
                        fragment = new MyAddsFragment();
                        break;

                    case R.id.navigation_profile:
                        fragment = new ProfileFragment();
                        break;


                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
                return true;
            }
        };

        @Override
        public void onNavigationItemReselected(@NonNull MenuItem menuItem) {

        }
    }

和XML 元素结构像这样放置:

<DrawerLayout>
    <FrameLayout/>
    <NavigationView/>
</DrawerLayout>

请尝试使用 fragment.add() 方法。