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

相关内容

热门资讯

我来向大家传授“约局吧黑科技辅... 我来向大家传授“约局吧黑科技辅助器”agpoker辅助(原生真的是有挂)是一款可以让一直输的玩家,快...
盘点一款“天天微友有辅助麻”w... 盘点一款“天天微友有辅助麻”wejoker辅助脚本(果然是有挂);1分钟了解详细教程(微信 1367...
一分钟揭秘“传送屋激k看底牌辅... 一分钟揭秘“传送屋激k看底牌辅助软件”wpk模拟器(确实真的是有挂);支持2-10人实时对战,虚拟庄...
解密关于“决战加血辅助”来玩a... 解密关于“决战加血辅助”来玩app 德州 辅助(真是是有挂)1、很好的工具软件,可以解锁游戏的ai质...
玩家必看分享“星悦软件辅助”h... 玩家必看分享“星悦软件辅助”hhpoker辅助挂(一贯真的有挂)1、很好的工具软件,可以解锁游戏的a...
重大通报“陕麻圈辅助器购买”a... 重大通报“陕麻圈辅助器购买”aapoker辅助插件工具(起初真的有挂)1、不需要AI权限,帮助你快速...
记者爆料“中指小程序破解头像”... 记者爆料“中指小程序破解头像”淘宝买wepoker透视有用吗(一贯有挂);致您一封信;亲爱中指小程序...
一分钟揭秘“福建十三水辅助”德... 一分钟揭秘“福建十三水辅助”德普之星透视辅助软件(原来真的是有挂);大神普及一款德州ai内幕,确定了...
1.9分钟了解“新悠悠拼十辅助... 1.9分钟了解“新悠悠拼十辅助”wepoker手机助手(一贯是有挂);支持2-10人实时对战,虚拟庄...
一分钟揭秘“新海贝之城辅助”h... 一分钟揭秘“新海贝之城辅助”hhpoker作弊实战视频(一向真的有挂);玩家必备必赢加哟《13670...