2

I have implement navigation drawer with onNavigationItemSelected. Here's my Main actvity :

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;


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

    drawerLayout = (DrawerLayout) findViewById(R.id.dl);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);

    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();



    //noinspection RestrictedApi
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item))
        return true;


    return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.masar)
        startActivity(new Intent(this, Masarat.class));
    else if (id == R.id.company)
        startActivity(new Intent(this, Masarat.class));
    else if (id == R.id.profile)
        startActivity(new Intent(this, Masarat.class));
    else if (id == R.id.logout)
        startActivity(new Intent(this, Masarat.class));

    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

}

The xml file of the Activity that contains the Home UI elements besides the Navigation Drawer:

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl"
xmlns:android="http://schemas.android.com/apk/res/android"
    >
<android.support.design.widget.NavigationView
    android:id="@+id/nv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/nav_said"
    app:headerLayout="@layout/header"
    android:layout_gravity="start"
    >
</android.support.design.widget.NavigationView>

<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/profiletransparent"
        android:scaleType="fitXY"
        />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/yo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>
    <TextView

        android:layout_below="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3rfne"
        android:textSize="40dp"
        android:textColor="#fff"
        android:gravity="center"
        android:layout_marginTop="70dp"
        android:textStyle="normal"/>

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

When item is clicked it should go to other activity , but it's not working (The Navigation Drawer is closing when i click on item )

What's the problem?

Yousef Ahmad
  • 63
  • 1
  • 3
  • 10
  • 2
    The `` needs to be listed last within the ``. That is, move it to after the ``. – Mike M. Feb 21 '18 at 23:50
  • @MikeM. it worked , but the app is crashes and doesn't got to the Activity , Could you please tell me where's the problem ? – Yousef Ahmad Feb 22 '18 at 00:10
  • Look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Feb 22 '18 at 00:21
  • @MikeM. i'm getting this error " Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference" – Yousef Ahmad Feb 22 '18 at 00:30
  • It seems like the problem is in`Masarat`. Are you trying to set up a `RecyclerView` there? If so, that's likely where the issue is, since there doesn't seem to be one in `MainActivity`. – Mike M. Feb 22 '18 at 00:34
  • Yes , so i have to define a reference for the RecyclerView in the main Activity ?@MikeM. – Yousef Ahmad Feb 22 '18 at 00:39
  • No, I'm saying that if the problem is in `Masarat`, you need to look in `Masarat` to fix it. You'll have to figure out why the `RecyclerView` you're calling `setLayoutManager()` on there is null. – Mike M. Feb 22 '18 at 00:41
  • @MikeM. okk i'll try to solve it :) – Yousef Ahmad Feb 22 '18 at 00:43
  • 1
    thank u @MikeM. i solved it ..3> – Yousef Ahmad Feb 22 '18 at 00:47
  • Hi! if any one face this issue check my answer here:https://stackoverflow.com/a/51452908/2788786 – Muhammad Usman Ghani Jul 21 '18 at 04:51

0 Answers0