I have an ActionBarActivity in TabActivity. when I call getSupportActionBar()'s methods like setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) and so on, the ActionBarActivity will throw NPE.
I spent a lot of time to search the stackoverflow but not work.
- My style is
Theme.AppCompat.Light - I did not hide the actionBar
- The actionBar is not null (I have debug it).
- all above works perfect on android 2.3 but 4.3.
The codes:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
bar = getSupportActionBar();
(line 38)bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
pager = (ViewPager) findViewById(R.id.news_pager);
urlGenerator = new UrlGenerator("getNewsClasses");
pagerAdapter = new NewsPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
bar.setSelectedNavigationItem(position);
}
});
}
The error:
Caused by: java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.setNavigationMode(ActionBarImplICS.java:229)
at android.support.v7.app.ActionBarImplJB.setNavigationMode(ActionBarImplJB.java:20)
at com.ccw.estate.news.NewsActivity.onCreate(NewsActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 24 more
The menifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="name"
android:label="label" >
</activity>
</application>
The AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
Who knows how it. thanks!
ActionBarImplICS.java?Activityas a tab in aTabActivity(if yes you shouldn't do this)?ActionBarActivityas a tab inTabActivitydoesn't properly initialize theActionBar. And what you are trying to do will not make any sense at all, it will be counter intuitive for the user and against any Android design principles. Also,TabActivityis deprecated, use fragments for what you currently try to do.