Mike49 Mike49 - 5 months ago 24
Android Question

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

Answer

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

((ActionBarActivity)getActivity()).getSupportActionBar()
Comments