切换菜单按钮不会打开滑动菜单并关闭应用程序

Toggle menu Button doesn't open sliding menu and shuts down app

我在我的应用程序中包含了 DrawerLayout。我在其中添加了工具栏和 "home button"。除了工具栏左侧的 "menu button" 之外,滑动菜单工作正常。问题是,当我点击它时,我的应用程序无故关闭。我不知道为什么它会关闭。它应该打开隐藏的滑动菜单。如果我从左向右滑动此菜单会显示,但当单击按钮时它会立即关闭所有应用程序。

下面是我的菜单XML代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include layout="@layout/activity_main_sliding_content_main" />
    <!--
   <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>-->

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>


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

和包含的视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:visibility="visible"/>


    <ListView
        android:id="@+id/listviewmain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/adView"
        android:layout_below="@+id/my_toolbar" />


    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/progressText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Name"
        android:layout_below="@+id/progressbar"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

JAVA 代码如下:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // setContentView(R.layout.activity_main);
        setContentView(R.layout.maincontent_slidingmenu);

        mainRelativeLayout = (RelativeLayout) findViewById(R.id.mainRelativeLayout);

       // mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        hiddenLayout = (RelativeLayout) findViewById(R.id.hiddenLayout);

        myToolbar = (Toolbar) findViewById(R.id.my_toolbar);

        listView = (ListView) findViewById(R.id.listviewmain);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        progressView = (TextView) findViewById(R.id.progressText);


        setSupportActionBar(myToolbar);

        setupSlidingMenu(savedInstanceState);
        setUpActionBarDrawerToggle();
        setOnClickListners();
        loadDataIntoMmemory();
        setUpAdds();
    }

private void setupSlidingMenu(Bundle savedInstanceState)
    {
        mTitle = mDrawerTitle = getTitle();

        menuTitles = getResources().getStringArray(R.array.titles);
        menuIcons = getResources().obtainTypedArray(R.array.icons);

        mDrawerList = (ListView) findViewById(R.id.left_drawer) ;

        rowItems = new ArrayList<RowItem>();

        for (int i = 0; i < menuTitles.length; i++) {
            RowItem items = new RowItem(menuTitles[i], menuIcons.getResourceId(
                    i, -1));
            rowItems.add(items);
        }

        menuIcons.recycle();

        customSlidingAdapter = new CustomSlidingMenuAdapter(getApplicationContext(), rowItems);
        System.out.println(customSlidingAdapter.isEmpty());

        mDrawerList.setAdapter(customSlidingAdapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        /*mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());*/



    };

    private void setUpActionBarDrawerToggle()
    {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        //new ActionBarDrawerToggle(myToolbar,mDrawerLayout, R.string.opened, R.string.zamkniecie);


        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.opened, R.string.zamkniecie) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                myToolbar.setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                syncState();
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                myToolbar.setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                syncState();
            }

            @Override
            public void setToolbarNavigationClickListener(View.OnClickListener onToolbarNavigationClickListener) {



                super.setToolbarNavigationClickListener(onToolbarNavigationClickListener);

                mDrawerLayout.openDrawer(mDrawerList);
            }
        };

        // Set the drawer toggle as the DrawerListener

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDrawerLayout.addDrawerListener(mDrawerToggle);

    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }






    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
                return true;

            case R.id.action_settings:
                // User chose the "Settings" item, show the app settings UI...
                return true;

            case R.id.action_favorite:
                // User chose the "Favorite" action, mark the current item
                // as a favorite...
                return true;

            case R.id.action_searching:

              /*//  MenuItem searchItem = menu.findItem(R.id.action_searching);
                //searchView = (SearchView) MenuItemCompat.getActionView();
                searchView.setOnQueryTextListener(this);*/
              return true;

            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.

                return super.onOptionsItemSelected(item);

        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        //getActionBar().setTitle(mTitle);
        myToolbar.setTitle(mTitle);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //menu.findItem(R.id.action_settings).setVisible(!drawerOpen);

        mDrawerToggle.syncState();

        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);


       // menu.findItem(R.id.left_drawer).setVisible(!drawerOpen);
       // return super.onPrepareOptionsMenu(menu);

        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {

        // Sync the toggle state after onRestoreInstanceState has occurred.
      //  mDrawerToggle.syncState();

        super.onPostCreate(savedInstanceState);

        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        // Pass any configuration change to the drawer toggls


        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        MenuItem searchItem = menu.findItem(R.id.action_searching);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(this);


        return true;

    }

将您的 setUpActionBarDrawerToggle 替换为以下内容并检查

private void setUpActionBarDrawerToggle()
    {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

         mDrawerToggle =   new ActionBarDrawerToggle(this, mDrawerLayout, myToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                myToolbar.setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                syncState();
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                myToolbar.setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                syncState();
            }

            @Override
            public void setToolbarNavigationClickListener(View.OnClickListener onToolbarNavigationClickListener) {



                super.setToolbarNavigationClickListener(onToolbarNavigationClickListener);

                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        };

        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
    }

您不需要添加

 getSupportActionBar().setHomeButtonEnabled(true);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);