jame
jame

Reputation: 132

android.support.v7.app.actionBar all method throw null pointer exception

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.

  1. My style is Theme.AppCompat.Light
  2. I did not hide the actionBar
  3. The actionBar is not null (I have debug it).
  4. 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!

Upvotes: 0

Views: 892

Answers (1)

Eric MaCarthy
Eric MaCarthy

Reputation: 552

Did you add the property (android:windowNoTitle) of ActionBarActivity? Adding "android:windowNoTitle" means no action bar is created. Therefore getSupportActionBar() will return null.

Upvotes: 3

Related Questions