我正在学习使用滑动菜单库。到目前为止,我可以使用带有 2 个片段的 viewpager。但我不能使用操作栏选项卡来导航这些片段。这是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPager = new ViewPager(this);
mPager.setId(R.id.pager);
mPager.setAdapter(new MyViewPagerAdapter(
getSupportFragmentManager()));
setContentView(mPager);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
getSlidingMenu().setTouchModeAbove(
SlidingMenu.TOUCHMODE_FULLSCREEN);
break;
default:
getSlidingMenu().setTouchModeAbove(
SlidingMenu.TOUCHMODE_MARGIN);
break;
}
}
});
mPager.setCurrentItem(0);
// Construct tab for action bar
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Pass the position on tab click to ViewPager
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub } }
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
mActionBar = getSupportActionBar();
tab1 = mActionBar.newTab();
suntime.setText("Sun Time");
suntime.setTabListener(tabListener);
tab2= mActionBar.newTab();
mapview.setText("View Map");
mapview.setTabListener(tabListener);
mActionBar.addTab(tab1);
mActionBar.addTab(tab2);
}
我遇到 getSupportActionBar() 空异常错误。任何人都可以帮忙吗?