Search code examples
javaandroidnullpointerexceptiontabsandroid-tablayout

TabLayout: How to start with the second tab?


I have a TabLayout in my app and I want the app to start at the second tab when it gets opened.

Here is my TabFragment.java:

public class TabFragment extends Fragment {
    public static TabLayout tabLayout;
    public static ViewPager viewPager;


    public static int int_items = 9;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        View v =  inflater.inflate(R.layout.tab_layout,null);
        tabLayout = (TabLayout) v.findViewById(R.id.tabs);
        tabLayout.getTabAt(1).select();
        viewPager = (ViewPager) v.findViewById(R.id.viewpager);
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));


        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                   }
        });
        return v;
    }


    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }


        // Tab positions
        @Override
        public Fragment getItem(int position) {
            if(position == 0){
                return new TabFavorite();
            }
            if(position == 1){
                return new Tab1();
            }
            if(position == 2){
                return new Tab2();
            }
            if(position == 3){
                return new Tab3();
            }
            if(position == 4){
                return new Tab8();
            }
            if(position == 5){
                return new Tab4();
            }
            if(position == 6){
                return new Tab5();
            }
            if(position == 7){
                return new Tab6();
            }
            if(position == 8){
                return new Tab7();
            }
        return null;
        }


        // Tab titles
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0 :
                    return getText(R.string.tabfavorite);
                case 1 :
                    return getText(R.string.tab1);
                case 2 :
                    return getText(R.string.tab2);
                case 3 :
                    return getText(R.string.tab3);
                case 4 :
                    return getText(R.string.tab8);
                case 5 :
                    return getText(R.string.tab4);
                case 6 :
                    return getText(R.string.tab5);
                case 7 :
                    return getText(R.string.tab6);
                case 8 :
                    return getText(R.string.tab7);

            }
                return null;
        }

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

I found following code on the Internet, it should do the job:

tabLayout.getTabAt(1).select();

But this is giving me following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.tabs.TabLayout$Tab.select()' on a null object reference
        at com.my.app.TabFragment.onCreateView(TabFragment.java:46)

What do I have to do that it starts at the second tab? I can't find any other solutions to this on the internet. Thank you very much


Solution

  • It looks like you're trying to access a tab before it exists. Try calling tabLayout.setupWithViewPager(viewPager); before selecting the tab.