Can't get Action Bar in fragment class

后端 未结 5 361
深忆病人
深忆病人 2020-12-15 16:11

I have an activity with three fragment classes inside it. I get an error when trying to change the action bar title from inside of them. If I try to make the classes just pu

相关标签:
5条回答
  • 2020-12-15 16:22

    @Robert Estivil If you are using AppCompatActivity then use this:

    actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    
    0 讨论(0)
  • 2020-12-15 16:31

    if you use SherlokActionBar you can called your action bar using

    ActionBar mActionBar = ((SherlockFragmentActivity) getActivity()).getSupportActionBar();
    
    0 讨论(0)
  • 2020-12-15 16:35

    A static class cannot see private fields of another class. You will need to use getActivity().getActionBar() to retrieve it.

    0 讨论(0)
  • 2020-12-15 16:38

    Though i am late in answering here. I found arul's answer perfect, but now ActionbarActivity is deprecated so slight modification to that answer will finish the job:

    ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    
    0 讨论(0)
  • 2020-12-15 16:39

    With AppCompatActivity it is advised to shift to Toolbar instead of the action bar, But if you are not doing so then use this to get an instance of action bar.

    ((AppCompatActivity )getActivity()).getSupportActionBar();
    
    0 讨论(0)
提交回复
热议问题