0

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!

8
  • What is line 229 in ActionBarImplICS.java? Commented Jun 25, 2014 at 9:42
  • Post the manifest file also Commented Jun 25, 2014 at 9:43
  • You are using this Activity as a tab in a TabActivity(if yes you shouldn't do this)? Commented Jun 25, 2014 at 9:44
  • @Raghunandan I search it on google but not found. it is android source code. Commented Jun 25, 2014 at 9:44
  • 1
    Rethink your approach. Apparently using an ActionBarActivity as a tab in TabActivity doesn't properly initialize the ActionBar. 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, TabActivity is deprecated, use fragments for what you currently try to do. Commented Jun 25, 2014 at 10:51

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.