Romain Sion
Romain Sion

Reputation: 77

ActionBar doesn't work with api level < 14

In my application, i use ViewPager with ActionBar which require api level > 11 . But it doesn't work for me with api level 11 , 12 and 13.

Errors come from this line ( 22 ):

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

And the error is :

11-17 14:34:41.795: E/AndroidRuntime(472): Caused by: java.lang.NullPointerException
11-17 14:34:41.795: E/AndroidRuntime(472):  at fr.carnet.free.ViewPagerActivity.onCreate(ViewPagerActivity.java:22)

Other code :

// Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

Thanks

Upvotes: 1

Views: 704

Answers (2)

Ben Weiss
Ben Weiss

Reputation: 17922

I'm assuming that you're calling getActionBar() to obtain your action bar. The documentation for getActionBar() clearly states:

The Activity's ActionBar, or null if it does not have one.

Also, the documentation of ActionBar states:

Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default. You may otherwise add the action bar by calling requestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with the windowActionBar property.

So you might want to call requestFeature(FEATURE_ACTION_BAR) in order to set an ActionBar to your activity if you're not using one of the Holo themes for your app.

Upvotes: 2

vinay kumar
vinay kumar

Reputation: 1449

The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.

http://developer.android.com/guide/topics/ui/actionbar.html

Upvotes: 0

Related Questions