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

相关内容

热门资讯

今天上午!拱趴大菠萝辅助神器(... 今天上午!拱趴大菠萝辅助神器(透视)财神十三章如何提高运气(总是有辅助插件)-哔哩哔哩进入游戏-大厅...
做出回应!hhpoker德州透... 做出回应!hhpoker德州透视挂(透视)新星游辅助软件(总是是真的辅助工具)-哔哩哔哩1、新星游辅...
此事引发网友热议!德州透视是真... 此事引发网友热议!德州透视是真的假的(透视)闲逸辅助功能在哪(都是真的有辅助软件)-哔哩哔哩1、闲逸...
现有说明如下!wpk免费辅助(... 现有说明如下!wpk免费辅助(透视)四川熊猫辅助软件(好像是真的辅助app)-哔哩哔哩一、四川熊猫辅...
透视有挂!红龙poker辅助平... 透视有挂!红龙poker辅助平台(透视)海盗来了辅助哪个好(本来有辅助下载)-哔哩哔哩1.海盗来了辅...
据监测!hhpoker有辅助吗... 据监测!hhpoker有辅助吗(透视)爱来510k辅助器(都是真的是有辅助工具)-哔哩哔哩运爱来51...
透视了解!德州hhpoker脚... 透视了解!德州hhpoker脚本(透视)中至九江黑科技app怎么下载(确实真的是有辅助平台)-哔哩哔...
透视能赢!aapoker辅助插... 透视能赢!aapoker辅助插件工具(透视)微乐透视辅助工具(其实真的有辅助神器)-哔哩哔哩1.微乐...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker安装教程(透视)杭州都莱大菠萝有挂吗(果然是有辅助软件)-哔哩哔哩1...
透视智能ai!淘宝买wepok... 透视智能ai!淘宝买wepoker透视有用吗(透视)玩吧辅助器(竟然是有辅助插件)-哔哩哔哩进入游戏...