9

Good evening. I have android:minSdkVersion="14" android:targetSdkVersion="16"

And I have a NullPointerException in onCreate method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tain);

    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();

    //Here is the error
    actionBar.setHomeButtonEnabled(false);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

        for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mCollectionPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }

}

I have a simillar code in other project and it works right. The versions of the libraries in both projects are simillar too. What may caused this exception and what can I change?

Thanks.

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
Valeriy
  • 785
  • 2
  • 10
  • 28

4 Answers4

17

In your manifest make sure your activity has (of similar):

<activity android:theme="@android:style/Theme.Holo">
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
4

your error is getting caused by the getActionBar() method.

Check this link:

getActionBar() returns null

Community
  • 1
  • 1
Andres L
  • 683
  • 9
  • 20
2

Remove android:theme="@style/AppTheme" in your manifest file

NagaRaju
  • 17
  • 6
0

Made following changes in AndroidManifest file works for me

 @android:style/Theme.Holo.Light.DarkActionBar
KH_AJU
  • 109
  • 2
  • 18