Search code examples
android-fragmentsandroid-viewpager

NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference


I am trying to implement viewpager with Tablayout. In this everything is fine working properly, but the one problem is when i come back from another screen view pager lost their position with Tablayout and when i scroll its crash the app.I find many solutions but all of these cannot work for this. Here is my code Adapter Class:-

    public class Portfolio_Adapter extends FragmentStatePagerAdapter {

        int tabCount;

        public Portfolio_Adapter(FragmentManager fm,int tabCount) {
            super(fm);

            this.tabCount=tabCount;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position){

                case 0:
                    Websites_Fragment websites_fragment=new Websites_Fragment();
                    return websites_fragment;

                case 1:
                    Mobileapplication_fragment mobileapplication_fragment=new Mobileapplication_fragment();
                    return mobileapplication_fragment;
                case 2:
                    Graphics_Fragment graphics_fragment=new Graphics_Fragment();
                    return graphics_fragment;

                default:
                    return null;
            }

        }

        @Override
        public int getCount() {
            return tabCount;
        }
    }

Fragment class:-

public class Portfolio_fragment extends Fragment {

        TabLayout tabLayout;
        ViewPager viewPager;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.portfolio_fragment, container, false);

            ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Portfolio");
            tabLayout = (TabLayout) view.findViewById(R.id.portfolio_tabLayout);
            tabLayout.setupWithViewPager(viewPager);

            tabLayout.addTab(tabLayout.newTab().setText("Websites"));
            tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
            tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


            viewPager = (ViewPager) view.findViewById(R.id.portfolio_pager);

            Portfolio_Adapter adapter = new Portfolio_Adapter(getChildFragmentManager(), tabLayout.getTabCount());

            viewPager.setAdapter(adapter);
            viewPager.setOffscreenPageLimit(3);

            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {

                    viewPager.setCurrentItem(tab.getPosition());

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

            ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                View tab = slidingTabStrip.getChildAt(i);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tab.getLayoutParams();
                layoutParams.weight = 1;
                tab.setLayoutParams(layoutParams);
            }

            return view;
        }

    }

Solution

  • Your code is right you only have to change the placement of code put your

    tabLayout.setupWithViewPager(viewPager);
    

    ,

    tabLayout.addTab(tabLayout.newTab().setText("Websites"));
    tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
    tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    

    after setadapter in viewPager

     viewPager.setAdapter(adapter);
     viewPager.setOffscreenPageLimit(3);