title: Android - Using AppCompat/ActionBar for older devices(java.lang.NullPointerException)
body: <p>I have an application which currently works on Android versions 4.x and more.
  I am trying to change it so it can be usable by older devices such as 2.x and more.
  Everything is working fine except the action bar. I am getting a null pointer exception every time I am trying to getSupportActionBar. This is am issue that have been discussed a lot in forums but I still cannot solve it.</p>
  
  <p>The application uses the appCompat library but it does not seem to work on older devices.</p>
  
  <p>styles.xml file</p>
  
  <pre><code> &lt;style name="AppBaseTheme" parent="Theme.MyApp"&gt;
      &lt;item name="android:spinnerItemStyle"&gt;@style/SpinnerItem&lt;/item&gt;
      &lt;item name="android:spinnerDropDownItemStyle"&gt;@style/SpinnerItem.DropDownItem&lt;/item&gt;
      &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt;
   &lt;/style&gt;
  </code></pre>
  
  <p>MyApp.xml file </p>
  
  <pre><code>&lt;style name="Theme.MyApp" parent="@style/Theme.AppCompat"&gt;
        &lt;item name="actionBarItemBackground"&gt;@drawable/selectable_background&lt;/item&gt;
        &lt;item name="popupMenuStyle"&gt;@style/PopupMenu.MyApp&lt;/item&gt;
        &lt;item name="dropDownListViewStyle"&gt;@style/DropDownListView.MyApp&lt;/item&gt;
        &lt;item name="actionBarTabStyle"&gt;@style/ActionBarTabStyle.MyApp&lt;/item&gt;
        &lt;item name="actionDropDownStyle"&gt;@style/DropDownNav.MyApp&lt;/item&gt;
        &lt;item name="actionBarStyle"&gt;@style/ActionBar.Transparent.MyApp&lt;/item&gt;
        &lt;item name="actionModeBackground"&gt;@drawable/cab_background_top&lt;/item&gt;
        &lt;item name="actionModeSplitBackground"&gt;@drawable/cab_background_bottom&lt;/item&gt;
        &lt;item name="actionModeCloseButtonStyle"&gt;@style/ActionButton.CloseMode.MyApp&lt;/item&gt;
  </code></pre>
  
  <p></p>
  
  <p>Activity trying to get the actionbar</p>
  
  <pre><code>  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_auction_list);
  
  
  
      if (android.os.Build.VERSION.SDK_INT &gt;=11)
      {
          getActionBar().setIcon(R.drawable.gem);
          getActionBar().setDisplayHomeAsUpEnabled(false);
          this.getActionBar().setDisplayShowCustomEnabled(true);
          this.getActionBar().setDisplayShowTitleEnabled(false);
  
          LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View v = inflator.inflate(R.layout.actionbar_title, null);
  
          ((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
  
          //assign the view to the actionbar
          this.getActionBar().setCustomView(v);
      }
     else
    {
  //This calls a private class inside the same class to get a supported action bar
             notActionBar nab = new notActionBar();
             nab.getNotActionBar(); //THIS IS THE ERROR LINE
  
  
  }
  </code></pre>
  
  <p>Private class that gets the supported action bar. I have used a private class instead of directly getting the actionbar because i could not extend the ActionBarActivity because the activity all ready extends fragments.</p>
  
  <pre><code>private class notActionBar extends ActionBarActivity {
      public void getNotActionBar(){
          ActionBar actionBar = getSupportActionBar(); //NULL POINTER EXCEPTION
          actionBar.setIcon(R.drawable.gem);
          actionBar.setDisplayHomeAsUpEnabled(false);
          actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
          actionBar.setDisplayShowCustomEnabled(true);
          this.getSupportActionBar().setDisplayShowCustomEnabled(true);
          this.getSupportActionBar().setDisplayShowTitleEnabled(false);
  
          LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View v = inflator.inflate(R.layout.actionbar_title, null);
  
          ((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
  
          //assign the view to the actionbar
          this.getSupportActionBar().setCustomView(v);
  
   }
  
      public void setTitleActionBar(String a){
  
          getSupportActionBar().setTitle(a);
  
  
      }
      }
  </code></pre>