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

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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

回答1:

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.



回答2:

I believe this 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.



回答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(); 


回答4:

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(); 


回答5:

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



回答6:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!