我有一個底部導航視圖,里面有 4 個專案。在其中一個片段(HomeFragment)中,我添加了帶有 onClickListener 的卡片視圖。
單擊 CardView 后,它將用戶移動到一個新的 Fragment。當 Fragment 膨脹時,BottomNavigationView 完全消失,并且在單擊后退按鈕時應用程式崩潰(在對 BottomNavigationView 的空參考上)。
錯誤:
Attempt to invoke virtual method 'android.view.Menu com.google.android.material.bottomnavigation.BottomNavigationView.getMenu()' on a null object reference
BottomNavigationView 的 XML:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/nav_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_nav_menu" />
來自 HomeFragment 的交易:
Fragment fragment = new NotificationsFragment();
getChildFragmentManager().beginTransaction()
.replace(R.id.fragment_home, fragment)
.addToBackStack("HomeFragment")
.commit();
附上 HomeFragment 的螢屏截圖,其中出現了 BottomNavigationView,以及新的 NotificationFragment - 沒有 BottomNavigationView。
HomeFragment(帶底部導航)

NotificationFragment(缺少底部導航)

預期結果:
- BottomNavigationView 應該出現。
編輯#1
main_activity.xml - 保存 BottomNavigationView 的片段
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/nav_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_nav_menu" />
<fragment
android:id="@ id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
class="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.java - 啟動片段事務
public class HomeFragment extends Fragment {
private static final int AUTOCOMPLETE_REQUEST_CODE = 3;
private static final String TAG = "HomeFragment";
private FragmentHomeBinding binding;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
CardView card = getView().findViewById(R.id.card1);
Log.e(TAG, "Found Card!");
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().setContentView(R.layout.fragment_notifications);
Log.e(TAG, "Starting Login Activity");
Fragment fragment = new NotificationsFragment();
getChildFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in,R.anim.slide_in)
.replace(R.id.fragment_home, fragment)
.addToBackStack("HomeFragment")
.commit();
}
});
}
編輯 2
使用以下方法解決:
NavHostFragment.findNavController(current).navigate(R.id.navigation_notifications);
當前:
Fragment current = this;
OnClickListener(當前片段)的外側。
uj5u.com熱心網友回復:
第一個問題是您使用getChildFragmentManager()而不是getFragmentManager(). 要與您HomeFragment的父級的片段容器進行互動,Activity您需要使用后者,并傳入容器 id.replace(R.id.nav_host_fragment_activity_main, fragment)
第二個問題是您嘗試手動管理Fragment事務,同時您在<fragment>標簽上使用導航組件,如下所示:app:navGraph="@navigation/mobile_navigation"
我建議只使用兩者之一,在本例中為Navigation Component。您只需執行以下操作即可在片段之間進行更改:
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e(TAG, "Starting Login Activity");
findNavController().navigate(R.id.action_home_to_notifications)
}
});
你在哪里mobile_navigation.xml有:
<fragment
android:id="@ id/destination_home"
android:name="...HomeFragment"
...>
<action
android:id="@ id/action_home_to_notifications"
app:destination="@id/destination_notifications"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/fade_out" />
</fragment>
<fragment
android:id="@ id/destination_notifications"
android:name="...NotificationFragment"
.../>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511233.html
