[英]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()
. 我在调用getActionBar()
时遇到空指针问题,我似乎已经通过使用getSupportActionBar()
解决了这个问题。 My app now runs but my nav drawer does not open. 我的应用程序现在运行但我的导航抽屉没有打开。
Here is my base activity, all other activities extend this BaseActivity: 这是我的基本活动,所有其他活动都扩展了这个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: 以下是BaseActivity视图的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>
My minSdkVersion is 16 and I'm using the standard @style/AppTheme
which uses Theme.AppCompat.Light as its parent. 我的minSdkVersion是16,我正在使用标准@style/AppTheme
,它使用Theme.AppCompat.Light作为其父级。
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. 我认为最好的方法是使用onOptionsItemSelected()
方法在选择项目时关闭抽屉。
@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()
如果您使用支持库,则必须将方法getActionBar()
更改为getSupportActionBar()
check my answer here: getActionBar() returns null 在这里检查我的答案: getActionBar()返回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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.