简体   繁体   中英

Android navigation drawer won't open

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!

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

在此输入图像描述

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM