底部导航不起作用

时间:2018-07-15 11:21:05

标签: android android-fragments android-activity bottomnavigationview

我的底部导航视图不起作用。当我单击项目时,我的片段无法加载。

beginTransaction()可能会产生 NullPointerException

这是我的活动:

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();
        }
    });

这是我的片段之一:

 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);


}

2 个答案:

答案 0 :(得分:0)

我认为您可能会得到NullPointerException,因为您试图在首先分配给片段的片段管理器之前访问它。

因此,您需要使用活动提供的内容。因此,在您的loadFragment方法中,执行以下操作:

之前:

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

之后:

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

答案 1 :(得分: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;
}

如果您尝试从未吸引到活动的片段中获取fragmentmanager,则您的fragmentmanger返回null。因此,更改您的代码

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

收件人

FragmentTransaction transaction = getFragmentManager().beginTransaction();