简体   繁体   English

在Tablayout中选择选项卡时IndexOutOfBoundsException

[英]IndexOutOfBoundsException when selecting tab in Tablayout

I get IndexOutOfBoundsException error when i want selecting the tab in the TabLayout : This is the initTablayout function , that called in the onCreate method: 当我想在TabLayout选择选项卡时,我得到IndexOutOfBoundsException错误:这是在onCreate方法中调用的initTablayout函数:

       //======================================== Init Tablayout and view pager
            private void initTabLayout() {
                mainActivityViewPager = (ViewPager) findViewById(R.id.view_pager_main_activity);


                mainActivityTabLayout = (TabLayout)findViewById(R.id.tab_layout_main_activity);
                TabLayout.Tab tab = mainActivityTabLayout.getTabAt(1);
                tab.select();

                FragmentManager manager = getSupportFragmentManager();
                MainActivityPagerAdapter adapter = new MainActivityPagerAdapter(manager);
                mainActivityViewPager.setAdapter(adapter);
                mainActivityTabLayout.setupWithViewPager(mainActivityViewPager);
                mainActivityViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mainActivityTabLayout));
                mainActivityTabLayout.setTabsFromPagerAdapter(adapter);
            }

I got this error where i choose index of selected tab on the getTabAt(1) method , i have three tab on the PagerAdapter class : 我得到了这个错误,我在getTabAt(1)方法中选择了选定选项卡的索引,我在PagerAdapter类上有三个选项卡:

public class MainActivityPagerAdapter extends FragmentStatePagerAdapter {

        //============================================ Constructor

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

        //============================================ GetItem Method ()
        @Override
        public Fragment getItem(int position) {
            Fragment frag = null;
            switch (position) {
                case 0:
                    frag = new Fragment_one();
                    break;
                case 1:
                    frag = new Fragment_two();
                    break;
                case 2:
                    frag = new Fragment_three();
                    break;

            }
            return frag;
        }

        //============================================= GetCount Method ()
        @Override
        public int getCount() {
            return 3;
        }

        //============================================= GetPageTitle

        @Override
        public CharSequence getPageTitle(int position) {
            String title = "";
            switch (position) {
                case 0:
                    title = "اول ";
                    break;
                case 1:
                    title = "دوم ";
                    break;
                case 2:
                    title = "سوم ";
                    break;

            }

            return title;
        }
    }

Complete error is : 完整错误是:

 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

It seem like you forget to addTab to your mainActivityTabLayout so your TabLayout will have 0 item. 看起来你忘了添加toTab到你的mainActivityTabLayout所以你的TabLayout将有0项。 Therefore, you got IndexOutOfBoundsException when you access to tab1 因此,访问tab1时会出现IndexOutOfBoundsException

mainActivityTabLayout.addTab(tabLayout.newTab().setText("1"));
mainActivityTabLayout.addTab(tabLayout.newTab().setText("2"));
mainActivityTabLayout.addTab(tabLayout.newTab().setText("3"));

Select tab3 选择tab3

Do following : 做以下事项:

mainActivityTabLayout.addTab(tabLayout.newTab().setText("3"),true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android支持设计库中的TabLayout中的IndexOutOfBoundsException - IndexOutOfBoundsException in TabLayout in android support design lib 选择不同的选项卡时如何在 ViewPager + TabLayout 中暂停片段? - How to pause a fragment in ViewPager + TabLayout when a different tab is selected? 在另一个选项卡上滑动时包含 recyclerview 丢失信息的 TabLayout - TabLayout containing recyclerview lost information when swiping on another tab 选项卡已选择计数器的TabLayout - TabLayout with Tab selected counter 无法为tabLayout添加标签 - cannot add tab for tabLayout 从Textview中选择文本时出错(java.lang.IndexOutOfBoundsException:setSpan(-1 ... -1)在0之前开始) - Error when selecting text from Textview (java.lang.IndexOutOfBoundsException: setSpan (-1 … -1) starts before 0) 如何用tablayout制作比例制表符? - How make proportional tab with tablayout? 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 将图标设置为选项卡 - Setting an icon As tab Indicator in tablayout TabLayout:如何从第二个选项卡开始? - TabLayout: How to start with the second tab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM