0
爲什麼會有這樣的代碼崩潰,我這樣做完全按谷歌的指引,但我上線添加標籤來行動吧
final ActionBar actionBar = getActionBar();
這裏得到一個空指針異常是完整的代碼。我這樣做的話,每個我所鏈接到這裏http://developer.android.com/training/implementing-navigation/lateral.html
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.pager_view);
//TabSetup
tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(tabPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
});
final ActionBar actionBar = getActionBar();
assert actionBar != null;
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
}
};
//ADD THE APP'S 3 TABS
actionBar.addTab(actionBar.newTab().setText("News").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Surveys").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Connect").setTabListener(tabListener));
}