Actionbar 中的抽屉图标在我单击时向我显示错误

drawer icon in Actionbar is show me a error when I click then

我正在使用 Android Studio 1.2

操作栏中的图标抽屉在我单击时给我一个错误,但如果我用手从左向右滑动打开它,它工作正常。

这是我的布局,左边是我的抽屉列表;选项列表在 listView "mimenu"

<RelativeLayout xmlns:android="xxxxxx"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:background="#ffffff"
    tools:context="xxxx">


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffffff">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        >

        <ListView
            android:id="@+id/listaxx"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            ></ListView>

    </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"

            android:layout_height="match_parent"
            android:layout_gravity="left"

            >

            <ListView
                android:id="@+id/mimenu"
                android:layout_width="match_parent"
                android:layout_below="@+id/profileBox"
                android:choiceMode="singleChoice"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:divider="#eee"
                android:dividerHeight="1dp"
                />


        </RelativeLayout>



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



</RelativeLayout>

在我的classjava

    public class ListaPat extends Activity {

        public ArrayList<Pat> lstPat= new ArrayList<Pat>();
        private Pat_Adaptador adaptador;
        private LinearLayout linearLayout;

        ArrayAdapter<CharSequence> navigationDrawerAdapter;
        ListView leftDrawerList;
        String[] leftSliderData = new String[]{"test1","test2"};

        private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_lista_patx);

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

            leftDrawerList = (ListView)findViewById(R.id.mimenu);
            navigationDrawerAdapter= new ArrayAdapter<CharSequence>(  ListaPat .this, android.R.layout.simple_list_item_1, leftSliderData);
            leftDrawerList.setAdapter(navigationDrawerAdapter);


            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);


            mDrawerToggle = new ActionBarDrawerToggle(
                    this,
                    mDrawerLayout,
                    R.drawable.ic_drawer,
                    R.string.drawer_open,
                    R.string.drawer_close) {


                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);

                }


                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                }
            };


            RellenarNoticias();
            InicializarLista();


            mDrawerLayout.setDrawerListener(mDrawerToggle);


            getActionBar().setDisplayShowHomeEnabled(true);
            getActionBar().setHomeButtonEnabled(true);

            mDrawerToggle.syncState();





        }

 public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
                    mDrawerLayout.closeDrawer(leftDrawerList);
                }
                else {
                    mDrawerLayout.openDrawer(leftDrawerList);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

您需要扩展 ActionbarActivity 或 AppCompatActivity,并在 Activity 的开头使用 Toolbar 小部件。在创建活动中,执行以下操作:

  public class YourActivity extends ActionBarActivity{
        ...
        ...
        private Toolbar mToolbar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_layout);

            mToolbar = (Toolbar) findViewById(R.id.toolbar); 
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);

            //Set toggle with toolbar now!
            mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                mToolbar,
                R.string.drawer_open,
                R.string.drawer_close) {


            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);

            }


            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
        };

对于位于 activity xml 中的工具栏(布局的第一个元素!)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:background="@color/primary_color"
    android:title="@string/app_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="CreateSomeStyleHereIfYouNeed"
    app:popupTheme="@android:style/Theme.Holo.Light"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

</android.support.v7.widget.Toolbar>

我解决了更改此功能的问题

 public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
                    mDrawerLayout.closeDrawer(leftDrawerList);
                }
                else {
                    mDrawerLayout.openDrawer(leftDrawerList);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

解决问题的新功能

@Override public boolean onOptionsItemSelected(MenuItem item) {if (mDrawerToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }