简体   繁体   English

getActionBar显示为空

[英]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 我正在使用extended Fragment_Activity,并且我希望显示操作栏,但是在我的程序中,getActionBar()返回null

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. 如果要实现包含动作栏的活动,则应改用ActionBarActivity类,该类是该类的子类,因此允许您在API级别7和更高级别使用Fragment API。

Change following line 更改以下行

public class Main_Activity extends Fragment_Activity

to

public class Main_Activity extends ActionBarActivity

and to get ActionBar call 并获得ActionBar调用

 getSupportActionBar();

Hope this will helps you. 希望这对您有帮助。

Extend ActionBarActivity instead of FragmentActivity. 扩展ActionBarActivity而不是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")));
        }


    }

尝试获取ActionBar

ActionBar actionBar = getSupportActionBar();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM