title: .getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation
body: <p><strong>Structure</strong></p>
  
  <p>(names of Activies/Fragments changed)</p>
  
  <p>Based on some advice written in <em>Android Programming: The Big Nerd Ranch Guide</em>, I have the following structure:</p>
  
  <ul>
  <li><code>MyListFragment extends ListFragment</code></li>
  <li><code>MyListActivity extends SimpleFragmentActivity</code></li>
  <li><code>SimpleFragmentActivity extends ActionBarActivity</code></li>
  </ul>
  
  <p><strong>MyListActivity.java</strong></p>
  
  <pre><code>public class MyListActivity extends SingleFragmentActivity {
  
      @Override
      protected Fragment createFragment() {
          return new MyListFragment();
      }
  }
  </code></pre>
  
  <p><strong>SingleFragmentActivity.java</strong></p>
  
  <pre><code>public abstract class SingleFragmentActivity extends ActionBarActivity {
  
      protected abstract Fragment createFragment ();
  
      @Override
      public void onCreate (Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_fragment);
  
          FragmentManager fm = getSupportFragmentManager();
          Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
  
          if (fragment == null) {
              fragment = createFragment();
              fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
          }
      }
  }
  </code></pre>
  
  <p>After having outlined and programmed the basic functionality of my app (including Contextual Action Bar, navigation, …) I wanted to make sure that the app is as backwards compatible as possible without too much additional effort. Therefore, I started using the AppCompat (com.android.support:appcompat-v7:20+) ActionBar.</p>
  
  <p>What baffled me right at the beginning was that—opposed to every tutorial or Android’s Developer guidelines I read—the getSupportActionBar method has never been available in Android Studio (I’m using Android Studio v0.8.2 and JRE 1.7_60). Whenever I type <code>getActivity().getSup</code>, the only suggestions I get are <code>getSupportFragmentManager</code> and <code>getSupportLoaderManager</code>. </p>
  
  <p>As a workaround, I create a member variable <code>ActionBar mActionBar</code> in <code>MyListFragment</code> and initialise it:</p>
  
  <pre><code>@Override
  public void onAttach (Activity activity) {
      super.onAttach(activity);
      mActionBar = ((ActionBarActivity) activity).getSupportActionBar();
  }
  </code></pre>
  
  <p>That seemed to do the trick on either Android 4.4.4 (Moto G) and 2.3.4 (LG E-510)—the only devices I have available.</p>
  
  <p>However—coming to the problem I have—on rotation the ActionBar assignment gets lost and the application crashes with a NullPointerException. Debugging the application and setting a breakpoint on the following line in <code>onCreate</code>:</p>
  
  <pre><code>mActionBar.setTitle(R.string.title_activity_my_list);
  </code></pre>
  
  <p>I found out that mActionBar is null after the rotation. Placing said line in <code>onCreateView</code> didn’t help.</p>
  
  <p><strong>What I tried</strong></p>
  
  <ul>
  <li>Placing the initialisation of <code>mActionBar</code> in
  
  <ul>
  <li><code>onAttach</code></li>
  <li><code>onActivityCreated</code></li>
  <li><code>onCreate</code>/<code>onCreateView</code> right before .setTitle gets called</li>
  </ul></li>
  </ul>
  
  <p>I’m out of ideas here and grateful for advice.</p>
  
  <p><strong>EDIT-0 (30.07.)</strong></p>
  
  <p>Exception being thrown (after editing according to <a href="https://stackoverflow.com/a/25037502/1791820">Alex’ suggestion</a>)</p>
  
  <pre><code>07-30 15:05:25.239    5049-5049/MYAPPNAME E/AndroidRuntime﹕ [Blue Error Handler] Make Debugging Report file for main
  java.lang.RuntimeException: Unable to start activity ComponentInfo{MYAPPNAME/MYAPPNAME.MYLISTACTIVITY}: java.lang.NullPointerException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1653)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
      at android.app.ActivityThread.access$1600(ActivityThread.java:117)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:130)
      at android.app.ActivityThread.main(ActivityThread.java:3737)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:507)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
      at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.NullPointerException
      at MYAPPNAME.MYLISTFRAGMENT.onCreate(MYLISTFRAGMENT.java:54)
      at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
      at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1896)
      at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:216)
      at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
      at MYAPPNAME.SingleFragmentActivity.onCreate(SingleFragmentActivity.java:22)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
      at android.app.ActivityThread.access$1600(ActivityThread.java:117)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:130)
      at android.app.ActivityThread.main(ActivityThread.java:3737)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:507)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><code>MyListFragment</code> line 54 (exception occured using either SingleFragmentActivity <em>and</em> ActionBarActivity as a cast)</p>
  
  <pre><code>((SingleFragmentActivity) getActivity()).getSupportActionBar().setTitle(R.string
              .title_activity_my_list);
  </code></pre>
  
  <p><code>SingleFragmentActivity</code> lines 20-22:</p>
  
  <pre><code>@Override
  public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  </code></pre>