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

相关内容

热门资讯

科普透视!pokemmo免费脚... 科普透视!pokemmo免费脚本,pokemmo手机辅助软件(透视)一直是有辅助攻略(哔哩哔哩)所有...
为了进一步!红龙poker辅助... 为了进一步!红龙poker辅助器免费观看,来玩德州破解器,果然存在有辅助工具(哔哩哔哩)1、每一步都...
解谜透视!德州局脚本,云扑克有... 解谜透视!德州局脚本,云扑克有透视吗(透视)一直一直都是有辅助神器(哔哩哔哩)1、该软件可以轻松地帮...
据统计!哈糖大菠萝能开挂吗,手... 据统计!哈糖大菠萝能开挂吗,手机游戏辅助脚本工具,果然存在有辅助神器(哔哩哔哩)手机游戏辅助脚本工具...
揭幕透视!德州圈脚本,哈糖大菠... 揭幕透视!德州圈脚本,哈糖大菠萝挂法(透视)果然一直总是有辅助软件(哔哩哔哩)1、哈糖大菠萝挂法透视...
据目击者称!hhpoker外开... 据目击者称!hhpoker外开挂靠谱吗,湘竞技辅助,一贯真的是有辅助插件(哔哩哔哩)进入游戏-大厅左...
有挂透视!智星菠萝辅助,智星德... 有挂透视!智星菠萝辅助,智星德州可以透视吗(透视)本来真的是有辅助技巧(哔哩哔哩)1、进入游戏-大厅...
据通报!智星菠萝有挂吗,海南琼... 据通报!智星菠萝有挂吗,海南琼崖海海南辅助,原来真的有辅助脚本(哔哩哔哩)1、全新机制【海南琼崖海海...
开挂透视!约局吧app有挂吗,... 开挂透视!约局吧app有挂吗,菠萝辅助器免费版的功能介绍(透视)一贯真的是有辅助神器(哔哩哔哩)1、...
据通报!we poker插件,... 据通报!we poker插件,闽乐乐辅助器免费版,真是是有辅助软件(哔哩哔哩)1、用户打开应用后不用...