以下是我的主要活动类:
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
,因为getSupportFragmentManager
是FragmentActivity
的方法,但如果我没有类,那就好了不扩展Activity
类?我是Android新手,不确定是否可以这样做?
答案 0 :(得分:2)
但是,如果我没有一个不属于主要活动类的课程,那就好了吗?
open
延伸FragmentActivity
。因此,您只是间接扩展主Activity
类。
答案 1 :(得分:0)
您的班级必须延长AppCompatActivity
而不是Activity
才能访问getSupportFragmentManager()
。