我正在尝试向片段添加底部导航,但它不起作用,因为我在运行时收到以下错误
FATAL EXCEPTION: main
Process: com.otemainc.m_kliniki, PID: 29940
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.otemainc.m_kliniki/com.otemainc.m_kliniki.controller.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2993)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3128)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1888)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6801)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #19: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at com.otemainc.m_kliniki.controller.fragments.HomeFragment.onCreateView(HomeFragment.java:33)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:557)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:114)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1455)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3034)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2945)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3148)
at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:410)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:271)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:142)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:136)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:247)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:226)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.otemainc.m_kliniki.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:53)
at com.otemainc.m_kliniki.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:47)
at com.otemainc.m_kliniki.controller.MainActivity.onCreate(MainActivity.java:38)
2024-10-23 08:06:51.946 29940-29940 AndroidRuntime com.otemainc.m_kliniki E at android.app.Activity.performCreate(Activity.java:7185) at android.app.Activity.performCreate(Activity.java:7176) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1285) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2973) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3128) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1888) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6801) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
基本上,我所做的是添加底部导航和片段容器视图,它将充当主机片段,如下所示
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
tools:context=".controller.fragments.HomeFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_activity_nav"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation_menu"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
我还创建了导航图,并使用与菜单项中相同的名称命名每个导航片段项 id,如下所示 //导航片段
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph.xml"
app:startDestination="@id/userDashboardFragment">
<fragment
android:id="@+id/userDashboardFragment"
android:name="com.otemainc.m_kliniki.controller.fragments.UserDashboardFragment"
android:label="fragment_user_dashboard"
tools:layout="@layout/fragment_user_dashboard" />
<fragment
android:id="@+id/calculatorFragment"
android:name="com.otemainc.m_kliniki.controller.fragments.CalculatorFragment"
android:label="fragment_calculator"
tools:layout="@layout/fragment_calculator" />
<fragment
android:id="@+id/reservationFragment"
android:name="com.otemainc.m_kliniki.controller.fragments.ReservationFragment"
android:label="fragment_reservation"
tools:layout="@layout/fragment_reservation" />
<fragment
android:id="@+id/walletFragment"
android:name="com.otemainc.m_kliniki.controller.fragments.WalletFragment"
android:label="fragment_wallet"
tools:layout="@layout/fragment_wallet" />
</navigation>
//The menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/userDashboardFragment"
android:icon="@drawable/baseline_home_filled"
android:title="@string/menu_home" />
<item
android:id="@+id/calculatorFragment"
android:icon="@drawable/sigma"
android:title="@string/calculator" />
<item
android:id="@+id/reservationFragment"
android:icon="@drawable/baseline_calendar_today"
android:title="@string/reservations"/>
<item
android:id="@+id/walletFragment"
android:icon="@drawable/baseline_person_outline"
android:title="@string/wallet" />
</menu>
最后在我的 HomeFragment.java 文件中,我添加了代码来初始化每个项目并设置导航控制器,如下所示
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
BottomNavigationView navigationView = root.findViewById(R.id.bottom_navigation_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.userDashboardFragment, R.id.calculatorFragment, R.id.reservationFragment, R.id.walletFragment)
.build();
AppCompatActivity appCompat = (AppCompatActivity) requireActivity();
NavHostFragment navHostFragment = (NavHostFragment) appCompat.getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_activity_nav);
NavController navController = Objects.requireNonNull(navHostFragment).getNavController();
NavigationUI.setupActionBarWithNavController((AppCompatActivity) this.requireActivity(), navController, appBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
return root;
}
尽管 IDE 上没有显示任何错误,但运行时代码会崩溃,当我设置调试点时,我意识到问题出在 navHostFragment 为空。我可能会缺少什么,关于如何成功将 navhostfragment 设置为fragment_home.xml 中的那个的任何想法? 在破碎线及其上方设置调试点后,以下是输出的屏幕截图
应该是
android:id="@+id/nav_graph"
而不是android:id="@+id/nav_graph.xml"