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);

相关内容

热门资讯

我来教教大家!凑一桌开挂,杭州... 我来教教大家!凑一桌开挂,杭州都莱游戏辅助,详细插件(真是有挂)1、杭州都莱游戏辅助脚本辅助下载、杭...
网友热议!pokemmo脚本最... 网友热议!pokemmo脚本最新版,德普之星辅助器怎么用,确实存在有挂(有挂神器)1、金币登录送、破...
发现一款!哈局八张辅助,微信小... 发现一款!哈局八张辅助,微信小程序辅助新天道辅助,详细脚本(有挂功能)1、下载好微信小程序辅助新天道...
近年来!wepoker私人局透... 近年来!wepoker私人局透视,智星菠萝透视,总是真的有挂(有挂攻略)1、起透看视 智星菠萝透视辅...
玩家必看秘籍!蛮王大厅脚本辅助... 玩家必看秘籍!蛮王大厅脚本辅助,创思维激k有挂吗,详细器(有挂秘笈)1、操作简单,无需蛮王大厅脚本辅...
推出新举措!佛手大菠萝有挂吗,... 推出新举措!佛手大菠萝有挂吗,智星德州插件,其实存在有挂(有挂秘籍)1、任何智星德州插件透视是真的假...
揭秘攻略!闲来透视辅助器最新版... 揭秘攻略!闲来透视辅助器最新版,欢聚水鱼天下辅助,详细技巧(有挂解密)1、游戏颠覆性的策略玩法,独创...
刚刚!wepokerplus辅... 刚刚!wepokerplus辅助,德扑之星私人局辅助器,总是真的有挂(确实有挂)1、任何德扑之星私人...
玩家必备攻略!陕西三代二破解,... 玩家必备攻略!陕西三代二破解,微信小程序中至上饶510k辅助器,详细器(确实有挂)一、微信小程序中至...
黑科技攻略!wepoker私人... 黑科技攻略!wepoker私人局怎么玩,德扑之星私人局辅助器,原来是有挂(有挂方式)小薇(辅助器软件...