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透视app下载"其实存在有辅助脚本(哔哩哔哩)1、上手简单,内置详细流程视频...
透视曝光"wepok... 透视曝光"wepoker私人局有透视吗"总是真的是有辅助攻略(哔哩哔哩)1.wepoker私人局有透...
透视有挂"aa po... 透视有挂"aa poker辅助包"一贯存在有辅助app(哔哩哔哩)1、每一步都需要思考,不同水平的挑...
透视总结"wepok... 透视总结"wepoker透视有用吗"果然真的是有辅助软件(哔哩哔哩)1、该软件可以轻松地帮助玩家将w...
透视曝光"xpoke... 透视曝光"xpoker辅助神器"确实存在有辅助方法(哔哩哔哩)1、许多玩家不知道xpoker辅助神器...
透视了解"wepok... 透视了解"wepoker黑侠辅助器"本来真的有辅助攻略(哔哩哔哩)1、wepoker黑侠辅助器破解器...
透视关于"sohoo... 透视关于"sohoo开挂辅助"总是是真的辅助器(哔哩哔哩)1、起透看视 sohoo开挂辅助辅助软件价...
透视详情"hhpok... 透视详情"hhpoker怎么防作必弊"总是真的有辅助工具(哔哩哔哩)1)hhpoker怎么防作必弊免...
透视揭露"pokem... 透视揭露"pokemmo辅助官网"本来是有辅助软件(哔哩哔哩)1、下载好pokemmo辅助官网透视辅...
透视关于"智星德州插... 透视关于"智星德州插件"切实真的有辅助方法(哔哩哔哩)1、智星德州插件模拟器是什么优化,智星德州插件...