Poueeerr
Poueeerr

Reputation: 27

When click on the menu icon to open NavigationDrawer it crashes

When i click on the icon to open the NavigationDrawer my app suddenly crashes and i am trying to solve this about 3 days. Can anyone help me? I am using Android Studio and this is my first app. My problem is to open the NavigationDrawer by clicking on the icon on app bar.

Codes:

MainActivity.java

    package com.example.chatapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;

FirebaseAuth mAuth;
FirebaseUser mUser;
DatabaseReference mUserRef;

String profileImageUriV, usernameV;
CircleImageView profileImageHeader;
TextView usernameHeader;



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

    toolbar=findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Chat App");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);

    mAuth=FirebaseAuth.getInstance();
    mUser=mAuth.getCurrentUser();
    mUserRef= FirebaseDatabase.getInstance().getReference().child("Users");


    drawerLayout=findViewById(R.id.drawerLayout);
    navigationView=findViewById(R.id.navView);

    View view=navigationView.inflateHeaderView(R.layout.drawer_header);
    profileImageHeader=view.findViewById(R.id.profileImage_Header);
    usernameHeader=view.findViewById(R.id.usernameHeader);

    navigationView.setNavigationItemSelectedListener(this);



}

@Override
protected void onStart() {
    super.onStart();
    if (mUser==null)
    {
        SendUserToLoginActivity();
    }
    else{
       mUserRef.child(mUser.getUid()).addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                {
                    profileImageUriV=dataSnapshot.child("profileImage").getValue().toString();
                    usernameV=dataSnapshot.child("username").getValue().toString();
                    Picasso.get().load(profileImageUriV).into(profileImageHeader);
                    usernameHeader.setText(usernameV);
                }
           }

           @Override
           public void onCancelled(@NonNull DatabaseError databaseError) {

               Toast.makeText(MainActivity.this, "Sorry! Something going wrong", Toast.LENGTH_SHORT).show();
           }
       });
    }

}

private void SendUserToLoginActivity() {
    Intent intent=new Intent(MainActivity.this, LoginActivity.class);
    startActivity(intent);
    finish();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()){
        case R.id.home:
            Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
            break;

        case R.id.profile:
            Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
            break;

        case R.id.friend:
            Toast.makeText(this, "Friend", Toast.LENGTH_SHORT).show();
            break;

        case R.id.findFriend:
            Toast.makeText(this, "Find Friend", Toast.LENGTH_SHORT).show();
            break;

        case R.id.chat:
            Toast.makeText(this, "Chat", Toast.LENGTH_SHORT).show();
            break;

        case R.id.logout:
            Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
            break;
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId()==android.R.id.home){
        drawerLayout.openDrawer(GravityCompat.START);
        return true;
    }
    return true;
}

}

Drawer_Menu.xml:

<group android:id="@+id/first">

    <item android:id="@+id/home"
        android:title="Home"
        android:icon="@drawable/ic_home"/>

    <item android:id="@+id/profile"
        android:title="Profile "
        android:icon="@drawable/ic_profile"/>

    <item android:id="@+id/friend"
        android:title="Friend "
        android:icon="@drawable/ic_friend"/>

    <item android:id="@+id/findFriend"
        android:title="Find Friend "
        android:icon="@drawable/ic_find_friend"/>

    <item android:id="@+id/chat"
        android:title="Chat "
        android:icon="@drawable/ic_chat"/>


</group>
<group android:id="@+id/second">
    <item android:id="@+id/logout"
        android:title="Logout "
        android:icon="@drawable/ic_logout"/>
</group>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/app_bar"
        layout="@layout/main_appbar" />

    <EditText
        android:id="@+id/inputAddPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/input_add_post_bg"
        android:hint="Post something here :)"
        android:paddingLeft="50dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar" />

    <ImageView
        android:id="@+id/addImagePost"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/inputAddPost"
        app:layout_constraintStart_toStartOf="@+id/inputAddPost"
        app:layout_constraintTop_toTopOf="@+id/inputAddPost"
        app:srcCompat="@drawable/ic_add_post_image" />

    <ImageView
        android:id="@+id/send_post_imageView"
        android:layout_width="53dp"
        android:layout_height="48dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/inputAddPost"
        app:layout_constraintEnd_toEndOf="@+id/inputAddPost"
        app:layout_constraintTop_toTopOf="@+id/inputAddPost"
        app:srcCompat="@drawable/send_post" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputAddPost"
        android:background="@color/colorGray"/>
</androidx.constraintlayout.widget.ConstraintLayout>


<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"/>


</androidx.drawerlayout.widget.DrawerLayout>

build.gradle:

implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-core:17.3.0'
implementation 'com.google.firebase:firebase-storage:19.1.1'

implementation 'com.firebaseui:firebase-ui-database:6.2.1'

implementation 'com.google.android.material:material:1.0.0'

implementation 'de.hdodenhof:circleimageview:3.1.0'

implementation 'com.squareup.picasso:picasso:2.71828'

Upvotes: 0

Views: 37

Answers (1)

vishal N
vishal N

Reputation: 162

I think You would have missed mentioning the id of the DrawerLayout in your 'activity_main.xml' through which our activity finds the Layout in the XML file

android:id="@+id/drawerLayout"

Below is the code after updating.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/app_bar"
        layout="@layout/main_appbar" />

    <EditText
        android:id="@+id/inputAddPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/input_add_post_bg"
        android:hint="Post something here :)"
        android:paddingLeft="50dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar" />

    <ImageView
        android:id="@+id/addImagePost"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/inputAddPost"
        app:layout_constraintStart_toStartOf="@+id/inputAddPost"
        app:layout_constraintTop_toTopOf="@+id/inputAddPost"
        app:srcCompat="@drawable/ic_add_post_image" />

    <ImageView
        android:id="@+id/send_post_imageView"
        android:layout_width="53dp"
        android:layout_height="48dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/inputAddPost"
        app:layout_constraintEnd_toEndOf="@+id/inputAddPost"
        app:layout_constraintTop_toTopOf="@+id/inputAddPost"
        app:srcCompat="@drawable/send_post" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputAddPost"
        android:background="@color/colorGray"/>
</androidx.constraintlayout.widget.ConstraintLayout>


<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"/>


</androidx.drawerlayout.widget.DrawerLayout>

Upvotes: 1

Related Questions