getSupportActionBar() The method getSupportActionBar() is undefined for the type TaskActivity. Why?
Asked Answered
S

7

25

I was recommended to extend my Activity class from ActionBarActivity

Here is the previous code:

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;  

public class MainActivity extends Activity  {

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

I wrote new application and followed advice.

import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;            

    public class MainActivity extends ActionBarActivity {

          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActionBar actionBar =getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);            
            setContentView(R.layout.activity_main);
          }

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
          }            
        }

If I use ACtionBarActivity instead of Activity, I get the following error on the phone, when I try to run it:

The method getSupportActionBar() is undefined for the type TaskActivity

Showdown answered 27/7, 2013 at 0:49 Comment(0)
S
2

Here is the answer of my question. I've asked that again with some remarks. How to add support libraries?

Showdown answered 1/8, 2013 at 12:24 Comment(1)
Does that make this question a duplicate?Outfall
E
79

Your class needs to extend from ActionBarActivity, rather than a plain Activity in order to use the getSupport*() methods.

Update [2015/04/23]: With the release of Android Support Library 22.1, you should now extend AppCompatActivity. Also, you no longer have to extend ActionBarActivity or AppCompatActivity, as you can now incorporate an AppCompatDelegate instance in any activity.

Explanatory answered 27/7, 2013 at 1:7 Comment(2)
@jj_: Well, because the getSupport*() methods simply don't exist in Activity, but rather live in FragmentActivity, which is an extension of the former and an ancestor of ActionBarActivity. If that doesn't make sense, just look up the JavaDoc and methods of each class.Explanatory
AppCompatActivity brings whole new range of issues.Avow
K
5

Here is another solution you could have used. It is working in my app.

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        android.support.v7.app.ActionBar actionBar =getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);            
        setContentView(R.layout.activity_main)

Then you can get rid of that import for the one line ActionBar use.

Kroll answered 14/5, 2014 at 20:25 Comment(2)
Thanks a lot, but I've already solved my problem. I've read about adding support libraries.Anderer
Why 5 upvotes for this? It's not a solution to Activity not having a getSupportActionBar() method, it's just a comment about code style...Homelike
S
3

If you are already extending from ActionBarActivity and you are trying to get the action bar from a fragment:

ActionBar mActionBar = (ActionBarActivity)getActivity()).getSupportActionBar();
Steam answered 13/2, 2015 at 16:12 Comment(0)
S
2

Here is the answer of my question. I've asked that again with some remarks. How to add support libraries?

Showdown answered 1/8, 2013 at 12:24 Comment(1)
Does that make this question a duplicate?Outfall
D
2

If you are extending from an AppCompatActivity and are trying to get the ActionBar from the Fragment, you can do this:

ActionBar mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
Drusilla answered 12/8, 2015 at 23:17 Comment(0)
B
1

You have to change the extends Activity to extends AppCompactActivity then try set and getSupportActionBar()

Bryon answered 22/2, 2018 at 4:26 Comment(0)
H
0

Can you set the ActionBar before you set the Contient View? This order would be better:

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

    ActionBar actionBar =getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);            
  }
Hallock answered 31/7, 2019 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.