Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method getActivity() in a class extending FragmentActivity

I have extended Fragment Activity in a class where I also need to have a custom toolbar. So,I added the code to get the toolbar but the setSupportActionBar(toolbar) did not work.Then, I added AppCompatActivity.getActivity() to cast but that did not work as well.

Here is the code -

public class main_fragment extends FragmentActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar toolbar;
private FragmentDrawer drawerFragment;


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

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);

}
like image 316
TeeKay Avatar asked Apr 16 '26 13:04

TeeKay


1 Answers

AppCompatActivity extends FragmentActivity. The first thing you have to change is

extends FragmentActivity

with

extends AppCompatActivity

getActivity() is a method of Fragment. Activity has not that method, so you don't need to call it to use setSupportActionBar and getSupportActionBar

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
like image 161
Blackbelt Avatar answered Apr 18 '26 05:04

Blackbelt