How can I implement fragments in my drawer menu

0

I'm trying to use the example of NavigationDrawer of Android Studio. I even managed to understand the code, I have refactored it in my project, but I can not change the fragments when some menu item is triggered, Android Studio does not point to any errors in the code but the fragment is not called, the activity code: p>

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stalking_app);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();

        Toast.makeText(StalkingApp.this, item.getTitle(), Toast.LENGTH_LONG).show();

        Fragment fragment = null;

        Class fragmentClass;

        fragmentClass = Fragment1.class;

        if (id == R.id.nav_procurar) {
            // Handle the camera action
            fragmentClass = Fragment1.class;
        } else if (id == R.id.nav_feed) {
            fragmentClass = Fragment1.class;
        } else if (id == R.id.nav_perfil) {
            fragmentClass = Fragment1.class;
        } else if (id == R.id.nav_fotos) {
            fragmentClass = Fragment1.class;
        } else if (id == R.id.nav_ferramentas) {
            fragmentClass = Fragment1.class;
        } else if (id == R.id.nav_sair) {
            fragmentClass = Fragment1.class;
        }


        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

Fragment code:

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
            return null;
        }

        return (RelativeLayout) inflater.inflate(R.layout.tab_layout_a, container, false);
    }

}

My container:

<LinearLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

It does not return an error, can anyone help me?

    
asked by anonymous 21.03.2016 / 20:53

0 answers