getSupportFragmentManager()无法解析方法

时间:2017-01-09 21:41:38

标签: android android-fragments

以下是我的主要活动类:

public class MainActivity extends Activity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //GETTING THE ERROR HERE
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    Master mView = new Master();
                    return mView;
                case 1:
                    Device dView = new Device();
                    return dView;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Master";
                case 1:
                    return "Device";
            }
            return null;
        }
    }
}

我收到错误的帖子mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());说无法解析方法getSupportFragmentManager。

我将MainActivity的基类从Activity更改为FragmentActivity,因为getSupportFragmentManagerFragmentActivity的方法,但如果我没有类,那就好了不扩展Activity类?我是Android新手,不确定是否可以这样做?

2 个答案:

答案 0 :(得分:2)

  

但是,如果我没有一个不属于主要活动类的课程,那就好了吗?

open延伸FragmentActivity。因此,您只是间接扩展主Activity类。

答案 1 :(得分:0)

您的班级必须延长AppCompatActivity而不是Activity才能访问getSupportFragmentManager()