title: SlidingMenu tutorial : getActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException on new activity creation
body: <p>I'm trying to do this tutorial : <a href="http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/" rel="nofollow noreferrer">http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/</a></p>
  
  <p>It is to make a sliding menu inside an app.</p>
  
  <p>Infortunately, the </p>
  
  <blockquote>
    <p><strong>getActionBar().setDisplayHomeAsUpEnabled(true);</strong></p>
  </blockquote>
  
  <p>is throwing a null pointer exception.</p>
  
  <p>I'm a beginner in android and I've read a couple of solutions but no one seems to work correctly for me and the application still crashes when it gets to this part of the code.</p>
  
  <p>There are the questions I read : 
  <a href="https://stackoverflow.com/questions/27078476/getactionbar-setdisplayhomeasupenabledtrue-throws-nullpointerexception">getActionBar().setDisplayHomeAsUpEnabled(true) throws NullPointerException</a>,               <a href="https://stackoverflow.com/questions/18602953/getactionbar-setdisplayhomeasupenabledtrue-throws-nullpointerexception-on-n">getActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException on new activity creation (Google - Basic Tutorial)</a>,
  <a href="https://stackoverflow.com/questions/27276303/getactionbar-setdisplayhomeasupenabledtrue-nullpointer-in-preferenceactivit">getActionBar().setDisplayHomeAsUpEnabled(true); nullpointer in preferenceActivity</a></p>
  
  <p>Can someone help me with this ? Apparently it deals with the Action Bar but I don't know what exactly ...</p>
  
  <p>It will be really helpful, thanks!</p>
  
  <p>Here is the code of the MainActivity that diplays the menu,
  and below it I provide you the error log 
  :</p>
  
  <blockquote>
    <p>package com.example.invite.myapplication.activities;</p>
  
  <pre><code>import com.example.invite.myapplication.adapter.NavDrawerListAdapter;
  import com.example.invite.myapplication.model.NavDrawerItem;
  import com.example.invite.myapplication.R;
  
  import com.example.invite.myapplication.frags.*;
  
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.app.Fragment;
  import android.app.FragmentManager;
  import android.content.res.Configuration;
  import android.content.res.TypedArray;
  import android.os.Bundle;
  import android.support.v4.app.ActionBarDrawerToggle;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  
  public class MainActivity extends Activity {
      private DrawerLayout mDrawerLayout;
      private ListView mDrawerList;
      private ActionBarDrawerToggle mDrawerToggle;
  
      // nav drawer title
      private CharSequence mDrawerTitle;×
  
      // used to store app title
      private CharSequence mTitle;
  
      // slide menu items
      private String[] navMenuTitles;
      private TypedArray navMenuIcons;
  
      private ArrayList&lt;NavDrawerItem&gt; navDrawerItems;
      private NavDrawerListAdapter adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mTitle = mDrawerTitle = getTitle();
  
          // load slide menu items
          navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
  
          // nav drawer icons from resources
          navMenuIcons = getResources()
                  .obtainTypedArray(R.array.nav_drawer_icons);
  
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  
          navDrawerItems = new ArrayList&lt;NavDrawerItem&gt;();
  
          // adding nav drawer items to array
          // Home
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
          // Find People
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
          // Photos
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
          // Communities, Will add a counter here
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
          // Pages
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
          // What's hot, We  will add a counter here
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
  
  
          // Recycle the typed array
          navMenuIcons.recycle();
  
          mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
  
          // setting the nav drawer list adapter
          adapter = new NavDrawerListAdapter(getApplicationContext(),
                  navDrawerItems);
          mDrawerList.setAdapter(adapter);
  
          // enabling action bar app icon and behaving it as toggle button
          **getActionBar().setDisplayHomeAsUpEnabled(true);**
          getActionBar().setHomeButtonEnabled(true);
  
          mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                  R.drawable.ic_drawer, //nav menu toggle icon
                  R.string.app_name, // nav drawer open - description for accessibility
                  R.string.app_name // nav drawer close - description for accessibility
          ) {
              public void onDrawerClosed(View view) {
                  getActionBar().setTitle(mTitle);
                  // calling onPrepareOptionsMenu() to show action bar icons
                  invalidateOptionsMenu();
              }
  
              public void onDrawerOpened(View drawerView) {
                  getActionBar().setTitle(mDrawerTitle);
                  // calling onPrepareOptionsMenu() to hide action bar icons
                  invalidateOptionsMenu();
              }
          };
          mDrawerLayout.setDrawerListener(mDrawerToggle);
  
          if (savedInstanceState == null) {
              // on first time display view for first nav item
              displayView(0);
          }
      }
  </code></pre>
  </blockquote>
  
  <p>Here the error log : </p>
  
  <blockquote>
    <p>06-11 02:12:24.364  25134-25134/com.example.invite.myapplication
    E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.invite.myapplication/com.example.invite.myapplication.activities.MainActivity}:
    java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                at android.app.ActivityThread.access$700(ActivityThread.java:159)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5419)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:525)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at com.example.invite.myapplication.activities.MainActivity.onCreate(MainActivity.java:91)
                at android.app.Activity.performCreate(Activity.java:5372)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                at android.app.ActivityThread.access$700(ActivityThread.java:159)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5419)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:525)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                at dalvik.system.NativeStart.main(Native Method)</p>
  </blockquote>
  
  <p>And here is the "styles.xml"</p>
  
  <blockquote>
    <p></p>
  
  <pre><code>&lt;!--
      Base application theme, dependent on API level. This theme is replaced
      by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
  --&gt;
  &lt;style name="AppBaseTheme" parent="android:Theme.Light"&gt;
      &lt;!--
          Theme customizations available in newer API levels can go in
          res/values-vXX/styles.xml, while customizations related to
          backward-compatibility can go here.
      --&gt;
  &lt;/style&gt;
  
  &lt;!-- Application theme. --&gt;
  &lt;style name="AppTheme" parent="AppBaseTheme"&gt;
      &lt;!-- All customizations that are NOT specific to a particular API-level can go here. --&gt;
  &lt;/style&gt;
  
  &lt;style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light"&gt;
      &lt;!-- &lt;item name = "android:windowActionBar"&gt;false&lt;/item&gt; --&gt;
      &lt;!-- &lt;item name = "android:windowNoTitle"&gt;true&lt;/item&gt; --&gt;
  &lt;/style&gt;
  </code></pre>
    
    <p></p>
    
    <h1>SOLUTION : # (hope it works for you too).</h1>
    
    <p>It was simple finally.</p>
    
    <p>Just add :</p>
    
    <blockquote>
      <p>requestWindowFeature(Window.FEATURE_ACTION_BAR);</p>
    </blockquote>
    
    <p>before SetContentView(View ..).</p>
    
    <p>As far as I understood, this kind of forces the Activity to request
    for an Action Bar "Action Bar please ! (?)".</p>
    
    <p>And set the min sdk 11 and target 17 in your manifest</p>
    
    <p>Here the answer that helped me finally :
    <a href="https://stackoverflow.com/a/10031400">https://stackoverflow.com/a/10031400</a></p>
  </blockquote>