簡體   English   中英

Android導航抽屜無法打開

[英]Android navigation drawer won't open

我檢查了其他類似的帖子,但找不到解決方案。 我在調用getActionBar()時遇到空指針問題,我似乎已經通過使用getSupportActionBar()解決了這個問題。 我的應用程序現在運行但我的導航抽屜沒有打開。

這是我的基本活動,所有其他活動都擴展了這個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視圖的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()返回null

修正了這個問題,打開抽屜就沒問題了

在此輸入圖像描述

用於代碼的導入:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM