0

My bottom navigation view not working. When i click on items my fragment in not loading.

beginTransaction() may produce NullPointerException.

This is my Activity :

public static void enableNavigation(Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager){
    view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()){
                case R.id.nav_home:
                    fragment = new FragmentMarker();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_bookmark:
                    fragment = new FragmentBookmark();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_blog:
                    fragment = new FragmentBlog();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_notification:
                    fragment = new FragmentNotification();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_account:
                    fragment = new FragmentAccount();
                    loadFragment(fragment);
                    return true;

            }
           return false;
        }

        private void loadFragment(Fragment fragment) {
            // load fragment
            FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });

And this is one of my fragments :

 private void setupBottomNavigationView(){
    Log.d(TAG, "setupBottomNavigationView: Setting up BottomNavigationView");
    BottomNavigationViewEx bottomNavigationViewEx = getView().findViewById(R.id.bottom_navigation);
    BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
    BottomNavigationViewHelper.enableNavigation(getContext(), bottomNavigationViewEx, getFragmentManager());
    Menu menu = bottomNavigationViewEx.getMenu();
    MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
    menuItem.setChecked(true);


}
plase
  • 361
  • 1
  • 3
  • 9

2 Answers2

0

I think you may be getting NullPointerException because you are trying to access a fragment's fragment manager before it has been allocated to one in the first place.

So, you need to use the one provided by the activity. So inside your loadFragment method do the following:

Before:

private void loadFragment(Fragment fragment) {
            // load fragment
            FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }

After:

 private void loadFragment(Fragment fragment) {
                // load fragment
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                transaction.replace(R.id.container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
RhysP
  • 104
  • 5
  • `BottomNavigationViewHelper.this` cannot be referenced from a static context – Max Yablonskyi Jul 15 '18 at 12:02
  • Just looked at your code again and noticed that your enableNavigation() method provides you with a FragmentManager instance called "supportFragmentManager". Please use this instead of calling getFragmentManager() – RhysP Jul 15 '18 at 12:09
  • still `BottomNavigationViewHelper.this` cannot be referenced from a static context – Max Yablonskyi Jul 15 '18 at 12:16
  • I've updated my answer code to demonstrate what I meant in the previous comment. – RhysP Jul 15 '18 at 12:22
0
/**
 * Return the FragmentManager for interacting with fragments associated
 * with this fragment's activity.  Note that this will be non-null slightly
 * before {@link #getActivity()}, during the time from when the fragment is
 * placed in a {@link FragmentTransaction} until it is committed and
 * attached to its activity.
 *
 * <p>If this Fragment is a child of another Fragment, the FragmentManager
 * returned here will be the parent's {@link #getChildFragmentManager()}.
 */
@Nullable
final public FragmentManager getFragmentManager() {
    return mFragmentManager;
}

Your fragmentmanger returns null if you try to get fragmentmanager from fragment that is not attched to activity. So change your code

FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();

To

FragmentTransaction transaction = getFragmentManager().beginTransaction();
Mohamed Mohaideen AH
  • 2,527
  • 1
  • 16
  • 24