Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in ActionBar.setHomeButtonEnabled

Tags:

java

android

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.

like image 291
Valeriy Avatar asked Mar 28 '13 13:03

Valeriy


2 Answers

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

<activity android:theme="@android:style/Theme.Holo">
like image 67
Bill Mote Avatar answered Nov 14 '22 12:11

Bill Mote


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

Check this link:

getActionBar() returns null

like image 4
Andres L Avatar answered Nov 14 '22 12:11

Andres L