XML : navigation drawer move between fragments

I'm having a problem with an app that I'm making with the main activity set as navigation drawer activity,
What I'm getting is that when i press an item in the navigation list, the fragment is not being replaced by the corresponding fragment.

The code for the replacement is the following:

      public boolean onNavigationItemSelected(MenuItem item) {      // Handle navigation view item clicks here.      int id = item.getItemId();      Fragment objFragment = null;      if (id == R.id.nav_dashboard) {          objFragment = new Dashboard_fragment();      } else if (id == R.id.nav_myschedule){          objFragment = new Schedule_fragment();      } else if (id == R.id.nav_rides) {          objFragment = new Rides_fragment();      } else if (id == R.id.nav_vouchers) {          objFragment = new Vouchers_fragment();      } else if (id == R.id.nav_help) {          objFragment = new Help_fragment();      } else if (id == R.id.nav_settings) {          objFragment = new Settings_fragment();      }        FragmentManager fragmentManager = getSupportFragmentManager();      fragmentManager.beginTransaction()              .replace(R.id.content_frame, objFragment)              .commit();          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);      drawer.closeDrawer(GravityCompat.START);      return true;  }    

where each fragment is the following:

  package com.example.user.greenmiles;    import android.app.Activity;  import android.net.Uri;  import android.os.Bundle;  import android.support.v4.app.Fragment;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;      /**   * A simple {@link Fragment} subclass.   * Activities that contain this fragment must implement the   * {@link Dashboard_fragment.OnFragmentInteractionListener} interface   * to handle interaction events.   * Use the {@link Dashboard_fragment#newInstance} factory method to   * create an instance of this fragment.   */  public class Dashboard_fragment extends Fragment {    private OnFragmentInteractionListener mListener;    public static Dashboard_fragment newInstance() {      Dashboard_fragment fragment = new Dashboard_fragment();      return fragment;  }    public Dashboard_fragment() {      // Required empty public constructor  }    @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);  }    @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,                           Bundle savedInstanceState) {      // Inflate the layout for this fragment        getActivity().setTitle("Dashboard");      return inflater.inflate(R.layout.fragment_dashboard_fragment, container, false);  }    // TODO: Rename method, update argument and hook method into UI event  public void onButtonPressed(Uri uri) {      if (mListener != null) {          mListener.onFragmentInteraction(uri);      }  }    @Override  public void onAttach(Activity activity) {      super.onAttach(activity);      try {          mListener = (OnFragmentInteractionListener) activity;      } catch (ClassCastException e) {          throw new ClassCastException(activity.toString()                  + " must implement OnFragmentInteractionListener");      }  }    @Override  public void onDetach() {      super.onDetach();      mListener = null;  }    /**   * This interface must be implemented by activities that contain this   * fragment to allow an interaction in this fragment to be communicated   * to the activity and potentially other fragments contained in that   * activity.   * <p/>   * See the Android Training lesson <a href=   * "http://developer.android.com/training/basics/fragments/communicating.html"   * >Communicating with Other Fragments</a> for more information.   */  public interface OnFragmentInteractionListener {      // TODO: Update argument type and name      public void onFragmentInteraction(Uri uri);  }    }    

And where the xml file corresponding to the main activity is the following:

  <?xml version="1.0" encoding="utf-8"?>    
  <FrameLayout      android:id="@+id/content_frame"      android:layout_width="match_parent"      android:layout_height="match_parent" />      <include layout="@layout/app_bar_main" android:layout_width="match_parent"      android:layout_height="match_parent" />    <android.support.design.widget.NavigationView android:id="@+id/nav_view"      android:layout_width="wrap_content" android:layout_height="match_parent"      android:layout_gravity="start" android:fitsSystemWindows="true"      app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />    

and the xml file corresponding to the fragments is the following:

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context="com.example.user.greenmiles.Dashboard_fragment">    <!-- TODO: Update blank fragment layout -->  <TextView android:layout_width="match_parent" android:layout_height="match_parent"      android:text="Dashboard"      android:textSize="16pt" />    

Please I have been stuck in this for a couple of days without being able to solve it.

Thanks a lot

No comments:

Post a Comment