Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 636
Евгений Смирнов Avatar asked Jul 27 '13 00:07

Евгений Смирнов


People also ask

What is Getupportactionbar?

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.

What is action bar Android studio?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

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.

like image 158
MH. Avatar answered Oct 04 '22 09:10

MH.


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.

like image 20
dongemus Avatar answered Oct 04 '22 09:10

dongemus