0

I am trying to call a fragment that has viewpager so that i can slide through it. But when i'm trying to run the code i hit an error when i reach 'addOnTabSelectedListener()'. Not sure what to solve this. Can someone please guide me on this. Thank you so much!

The error:

Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.addOnTabSelectedListener(android.support.design.widget.TabLayout$OnTabSelectedListener)' on a null object reference

public class FragmentExplore extends Fragment {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    public FragmentExplore() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_explore, container, false);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
        mViewPager = view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        TabLayout tabLayout = view.findViewById(R.id.tabs);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        return view;
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    FragmentExploreActivities fragmentExploreActivities = new FragmentExploreActivities();
                    return fragmentExploreActivities;
                case 1:
                    FragmentExplorePeople fragmentExplorePeople = new FragmentExplorePeople();
                    return fragmentExplorePeople;
                case 2:
                    FragmentExplorePosts fragmentExplorePosts = new FragmentExplorePosts();
                    return fragmentExplorePosts;
                case 3:
                    FragmentExploreMedia fragmentExploreMedia = new FragmentExploreMedia();
                    return fragmentExploreMedia;

            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }
}
ADM
  • 20,406
  • 11
  • 52
  • 83
MRu
  • 1,225
  • 7
  • 20
  • 44
  • 1
    It would seem that the `` with ID `tabs` is not in the `fragment_explore` layout. – Mike M. Feb 27 '18 at 13:47
  • Its NPE . Check `tabLayout` exists or not in fragment. – ADM Feb 27 '18 at 13:48
  • post your xml for the fragment – dazza5000 Feb 27 '18 at 13:49
  • I see. The fragment does not have the tabs. Because the fragment is called when i click bottom navigation view button in the main activity. That is where the tabs located. Is there any way i can call this tabs in the fragment? – MRu Feb 27 '18 at 13:56
  • Technically, yes, but if the tabs are in the `Activity`, then the `Activity` should really be handling them. Are you reusing these tabs in other `Fragment`s? That is, can you not just include them in the `Fragment`? – Mike M. Feb 27 '18 at 13:59
  • 1
    I've done as you suggested Mike. To move the tabs in the fragment. It's better this way. Sorry for the trouble guys. Thank you! – MRu Feb 27 '18 at 14:05

0 Answers0