.getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation

Cast your getActivity() to ActionBarActivity/AppCompatActivity (depends what you are using) and you will have access to the support ActionBar.

((AppCompatActivity)getActivity()).getSupportActionBar();

or

((ActionBarActivity)getActivity()).getSupportActionBar();

Use this code in onActivityCreated(...) method instead of onAttach(...)


Now it should actually be:

((AppCompatActivity)getActivity()).setSupportActionBar();

because ActionBarActivity is deprecated.

But this is only if you must do this outside of an activity, where it's most commonly done.