Android 抽屉导航打不开

标签 android navigation-drawer

我已经检查了其他类似的帖子,但找不到解决方案。我在调用 getActionBar() 时遇到了空指针问题,我似乎已经通过使用 getSupportActionBar() 解决了这个问题。我的应用程序现在可以运行,但我的抽屉导航没有打开。

这是我的基本 Activity ,所有其他 Activity 都扩展了这个 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.  
 */

这是 BaseActivity View 的 XML:

<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>

我的 minSdkVersion 是 16,我使用的是标准 @style/AppTheme,它使用 Theme.AppCompat.Light 作为其父级。

代码运行正常,没有错误,但抽屉导航打不开。

非常感谢任何帮助,提前致谢!

最佳答案

我认为最好的方法是在选择项目时使用 onOptionsItemSelected() 方法关闭抽屉。

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

如果您正在使用支持库,则非常重要 您必须将方法 getActionBar() 更改为 getSupportActionBar()

在这里查看我的答案: getActionBar() returns null

修复了打开抽屉时没有问题

enter image description here

用于您的代码的导入:

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;

关于Android 抽屉导航打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218875/

相关文章:

android - Activity 中未显示工具栏和操作栏

Android 测量屏幕的百分比

android - 如何将信息传递给 Android Studio 中的导航 header

android - 如何使 android 抽屉导航可滚动与标题滚动

android - 如何在抽屉导航顶部创建工具栏(Android/kotlin)

navigation-drawer - 可以使用提供的参数调用以下函数。向上导航

android 测试应用程序..toast 不起作用

android - 如何一次使用多个标签过滤 Android adb shell logcat?

android - 在Android中将触摸事件传递给父 View 的onTouchListener

android - 侧面抽屉导航项目点击不起作用