2017-05-24 2 views
0

J'ai inclus DrawerLayout dans mon application. J'ai ajouté la barre d'outils et le "bouton d'accueil" dedans. Le menu coulissant fonctionne bien, sauf ce "bouton de menu" sur la gauche de la barre d'outils. Le problème est que lorsque je clique dessus, mon application s'arrête sans raison. Je ne sais pas pourquoi ça se ferme. Il devrait ouvrir le menu coulissant caché. Si je balaye de gauche à droite, ce menu s'affiche mais lorsque le bouton est cliqué, il arrête immédiatement toutes les applications.Menu à bascule Le bouton n'ouvre pas le menu coulissant et arrête l'application

Ci-dessous est mon menu code 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> 

ET included:

<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 CODE CI-DESSOUS:

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

    } 
+0

Regardez la trace de la pile pour déterminer la cause de l'accident. http://stackoverflow.com/questions/23353173 Si vous avez besoin d'aide supplémentaire, vous devrez [modifier] votre question pour fournir cette trace, ainsi que le code Java correspondant. –

+0

Le problème est qu'il se ferme sans aucune information dans la section du moniteur Android. Que dois-je faire dans cette situation? – DKM

+0

Trace après pile et classe Java utilisant cette disposition. I –

Répondre

1

remplacer votre setUpActionBarDrawerToggle avec ci-dessous et vérifiez

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(); 
    } 

vous n'avez pas besoin d'ajouter

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

Merci, j'ai essayé et ça marche bien maintenant. – DKM