1

I've checked other similar posts and can't find a solution. I have had trouble with a null pointer upon calling getActionBar(), I seemed to have solved that by using getSupportActionBar(). My app now runs but my nav drawer does not open.

Here is my base activity, all other activities extend this BaseActivity:

* This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
 * We are going to extend all our other activites from this BaseActivity so that every activity will have Navigation Drawer in it.
 * This activity layout contain one frame layout in which we will add our child activity layout.  
 */

Here is the XML for BaseActivity's view:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:theme="@android:style/Theme.WithActionBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#888888"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

My minSdkVersion is 16 and I'm using the standard @style/AppTheme which uses Theme.AppCompat.Light as its parent.

The code runs fine with no errors but the navigation drawer just won't open.

Any help is greatly appreciated, thanks in advance!

Programmer
  • 69
  • 2
  • 8
  • Any error message inside your LogCat? – Jorgesys May 13 '15 at 15:52
  • @Elenasys Not at all, app compiles and runs fine, I just can't get the drawer to open at all. – Programmer May 13 '15 at 15:53
  • Ok!, Are you trying to open the menu sliding it? or clicking some element of the menu? – Jorgesys May 13 '15 at 16:04
  • @Elenasys I'm trying both, I tried sliding it out and clicking an <- Arrow like this at the top, on the left side of the action bar. Neither does anything. Preferably I'd like the drawer to open via a slide. – Programmer May 13 '15 at 16:13

1 Answers1

5

I think the best way is using the onOptionsItemSelected() method to close the drawer when an item is selected.

@Override   public boolean onOptionsItemSelected(MenuItem item) {
    if(mDrawerLayout.isDrawerOpen(mDrawerList)){
        mDrawerLayout.closeDrawer(mDrawerList);
    }else {
        mDrawerLayout.openDrawer(mDrawerList);
    }                   
}

Very important if you are using the support library you must change the method getActionBar() to getSupportActionBar()

check my answer here: getActionBar() returns null

Fixed this you will have no problem opening the Drawer

enter image description here

imports used for your code:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;
Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268