ManikantaReddy
ManikantaReddy

Reputation: 55

getActionBar Showing null

 public class Main_Activity extends Fragment_Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
            pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
            ActionBar bar=getActionBar();

            if(getActionBar()!=null) {
                bar.setDisplayHomeAsUpEnabled(true);
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4a90e2")));
            }


        }

I am using extends Fragment_Activity and i want action bar to be displayed but in my program getActionBar() is returning null

Upvotes: 0

Views: 74

Answers (3)

Nagaraju V
Nagaraju V

Reputation: 2757

As for the docs

If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.

Change following line

public class Main_Activity extends Fragment_Activity

to

public class Main_Activity extends ActionBarActivity

and to get ActionBar call

 getSupportActionBar();

Hope this will helps you.

Upvotes: 1

Sukhwant Singh Grewal
Sukhwant Singh Grewal

Reputation: 495

try to get ActionBar with

ActionBar actionBar = getSupportActionBar();

Upvotes: 0

Darish
Darish

Reputation: 11501

Extend ActionBarActivity instead of FragmentActivity.

public class Main_Activity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        ActionBar bar=getActionBar();

        if(getActionBar()!=null) {
            bar.setDisplayHomeAsUpEnabled(true);
            bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4a90e2")));
        }


    }

Upvotes: 0

Related Questions