Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resolve getSupportActionBar() method in fragment

I have an activity that extends ActionBarActivity and hosts a single fragment shown here:

import android.support.v7.app.ActionBarActivity;

public class CrimePagerActivity extends ActionBarActivity{

When I try to make a call to

getSupportActionBar()

I get an error saying it cannot be resolved. I tried passing in

getActionBar() 

which produced a NullPointerException error at runtime.
Here is the code in question.

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime, container, false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        if(NavUtils.getParentActivityName(getActivity()) != null) {
            getActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
like image 784
Mike49 Avatar asked Mar 11 '15 01:03

Mike49


1 Answers

Resolved the issue by casting the return of getActivity() to ActionBarActivity:

((ActionBarActivity)getActivity()).getSupportActionBar()
like image 182
Mike49 Avatar answered Oct 13 '22 00:10

Mike49