Robert Mathew
Robert Mathew

Reputation: 115

ActionBarCompat in FragmentActivty

I am getting error "The method getSupportActionBar() is undefined for the type MainActivity"

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

AppSectionsPagerAdapter mAppSectionsPagerAdapter;

ViewPager mViewPager;

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


    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());


    final ActionBar actionBar = getSupportActionBar();


    //actionBar.setHomeButtonEnabled(false);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


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

            actionBar.setSelectedNavigationItem(position);
        }
    });

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

Please help me out. Does setHomeButtonEnabled() support?

Which is better ActionBarCompat or ActionBarSherlock?

Upvotes: 5

Views: 3227

Answers (2)

Charan
Charan

Reputation: 940

When application is using getSupportActionBar() your activity must extend from ActionBarActivity. It is defined in the Support Package which you have already added as I can see in your imports.

public class MainActivity extends ActionBarActivity
                          implements ActionBar.TabListener {
    // your code goes here
}

Upvotes: 4

Vitaliy
Vitaliy

Reputation: 81

Your class needs to extends ActionBarActivity. Because of ActionBarActivity extends from FragmentActivity, you can use Fragments.

Upvotes: 8

Related Questions