AndroidTabLayout无法实例化。
创始人
2024-08-21 14:30:21
0

通常出现此问题的原因是因为没有正确调用TabLayout的构造函数。创建TabLayout实例时需要传递Context参数。以下是一个示例代码,演示如何在ViewPager中使用TabLayout:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager = findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // Set up the TabLayout with ViewPager
        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    // ...

    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    private class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.tab_text_1);
                case 1:
                    return getString(R.string.tab_text_2);

相关内容

热门资讯

第八分钟辅助!闲逸辅助神器免费... 第八分钟辅助!闲逸辅助神器免费,手机微乐小程序游戏破解器(总算清楚开挂辅助插件)相信很多朋友都在电脑...
第十分钟辅助!微信小程序雀神广... 微信小程序雀神广东智能辅助是一款专注玩家量身打造的游戏记牌类型软件,在微信小程序雀神广东智能辅助这款...
第6分钟辅助!杭州边锋辅助软件... 第6分钟辅助!杭州边锋辅助软件,贪玩互娱辅助(透视有挂开挂辅助软件);杭州边锋辅助软件中的10万兆豆...
3分钟开挂!边锋干瞪眼脚本,天... 3分钟开挂!边锋干瞪眼脚本,天蝎大厅辅助(科技分享开挂辅助神器)您好:边锋干瞪眼脚本这款游戏可以开挂...
七分钟辅助!福建兄弟13水修改... 七分钟辅助!福建兄弟13水修改器,奇迹山西辅助器(每日必看开挂辅助脚本);相信小伙伴都知道这个福建兄...
5分钟开挂!赣牌圈控制牌型辅助... 5分钟开挂!赣牌圈控制牌型辅助器,aapoker怎么拿好牌(玩家必看科普开挂辅助平台)【无需打开直接...
7分钟辅助!新星游挂多少钱,小... 7分钟辅助!新星游挂多少钱,小闲南川手游辅助器(科技通报开挂辅助插件);最新版2026是一款经典耐玩...
1分钟透视!闲逸软件可以控制的... 1分钟透视!闲逸软件可以控制的吗,微信小程序微乐辅助器(最新技巧开挂辅助软件)1、下载安装好闲逸软件...
第5分钟辅助!情怀宜春怎么开挂... 第5分钟辅助!情怀宜春怎么开挂,天天微友开控制多少钱(科技介绍开挂辅助下载);天天微友开控制多少钱中...
第5分钟透视!福建天天开心技巧... 第5分钟透视!福建天天开心技巧,微乐陕西三代自建房挂(玩家必用开挂辅助软件)>>您好:软件加薇136...