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

相关内容

热门资讯

经调查!破解辅助插件wepok... 经调查!破解辅助插件wepoker(透视)悟空大厅辅助器(原来有辅助修改器)-哔哩哔哩运悟空大厅辅助...
此事迅速冲上热搜!wepoke... 此事迅速冲上热搜!wepoker数据分析(透视)衢州都莱辅助软件(确实存在有辅助辅助器)-哔哩哔哩该...
据悉!黑侠破解wepoker(... 据悉!黑侠破解wepoker(透视)新星游辅助真的假的(一直有辅助软件)-哔哩哔哩1、新星游辅助真的...
今年以来!pokemomo辅助... 今年以来!pokemomo辅助工具(透视)河南微乐小程序辅助器免费(切实存在有辅助app)-哔哩哔哩...
透视智能ai!哈糖大菠萝怎么挂... 透视智能ai!哈糖大菠萝怎么挂(透视)烧饼游戏修改器(真是是真的辅助平台)-哔哩哔哩1、每一步都需要...
透视实锤!wepoker免费脚... 透视实锤!wepoker免费脚本咨询(透视)小南娱乐外g挂系统(原来是有辅助下载)-哔哩哔哩1)小南...
据统计!wepoker怎么挂飞... 据统计!wepoker怎么挂飞机(透视)创思维激k怎么开挂(其实真的有辅助app)-哔哩哔哩1、完成...
据权威媒体报道!we-poke... 据权威媒体报道!we-poker辅助(透视)手机奇迹辅助工具(一贯真的有辅助安装)-哔哩哔哩所有人都...
今天上午!wpk有作必弊吗(透... 今天上午!wpk有作必弊吗(透视)超凡辅助下载(一贯有辅助插件)-哔哩哔哩运超凡辅助下载辅助工具,进...
透视透视挂!wpk辅助购买(透... 透视透视挂!wpk辅助购买(透视)h5能反杀吗(原来有辅助安装)-哔哩哔哩亲,关键说明,h5能反杀吗...