Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 346
Robert Mathew Avatar asked Aug 01 '13 09:08

Robert Mathew


2 Answers

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

like image 157
Vitaliy Avatar answered Oct 07 '22 07:10

Vitaliy


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
}
like image 32
Charan Avatar answered Oct 07 '22 08:10

Charan