0

I am trying to use the same navigation drawer for all my activities. Homepage will redirect to Dashboard if I click the Dashboard card view. If I open the navigation drawer in the Dashboard page, the application crashes and shows the following error.

Error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.drawerlayout.widget.DrawerLayout.isDrawerOpen(int)' on a null object reference
        at fyp.ui_activities.Homepage.closedrawer(Homepage.java:94)
        at fyp.ui_activities.Dashboard.onPause(Dashboard.java:62)

Homepage.java

public class Homepage extends AppCompatActivity {

DrawerLayout drawerLayout;
TextView navheader;
ImageView navimage;

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

    drawerLayout = findViewById(R.id.drawer_layout);


    navheader = findViewById(R.id.nav_headertitle);
    navimage = findViewById(R.id.nav_headerpic);

    navheader.setText("RVD System");
    navimage.setImageResource(R.drawable.titlehead);

    //opendashboard
    CardView dashboard = findViewById(R.id.dashboardbtn);
    dashboard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            redirectActivity(Homepage.this,Dashboard.class);
        }
    });

    //openscanpage
    CardView scanpage = findViewById(R.id.scanbtn);
    scanpage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            redirectActivity(Homepage.this,Scanpage.class);
        }
    });
}

protected void onStart() {
    super.onStart();
    //check if user is logged in
    checkCurrenUser();
}

protected void onPause() {
    super.onPause();
    closedrawer(drawerLayout);
}

public void ClickDrawer(View view){
    opendrawer(drawerLayout);
}

public static void opendrawer(DrawerLayout drawerLayout) {
    drawerLayout.openDrawer(GravityCompat.START);
}

public void ClickLogo(View view){
    closedrawer(drawerLayout);
}

public static void closedrawer(DrawerLayout drawerLayout) {
    //closer drawer layout
    //check condition
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        //when drawer is open
        drawerLayout.closeDrawer(GravityCompat.START);
    }
}

public void ClickReset(View view){
    redirectActivity(this,Resetpassword.class);
}

public void ClickAboutUs(View view){
    //display about page
    setContentView(R.layout.activity_scanpage);
}

public static void redirectActivity(Activity activity, Class classname){
    Intent intent = new Intent(activity, classname);
    activity.startActivity(intent);
}

public void ClickLogout(View view){
    //create an alert dialog
    logout(this);
}

public void logout(final Activity activity){
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle("Logout");
    builder.setMessage("Confirm Logout?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent logout = new Intent(activity, Login.class);
            Toast.makeText(activity,"Username: "+ FirebaseAuth.getInstance().getCurrentUser().getDisplayName()+ "\nLogout Successful",Toast.LENGTH_SHORT).show();
            FirebaseAuth.getInstance().signOut();
            logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(logout);
            finish();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.show();

}

public void checkCurrenUser() {
    FirebaseUser fuser = FirebaseAuth.getInstance().getCurrentUser();
    if(fuser !=null){
        TextView displayname = findViewById(R.id.displayname_textview);
        String name = fuser.getDisplayName();
        displayname.setText("Welcome! "+ name);
    }
    else{
        Toast.makeText(getApplicationContext(), "No User Logged In", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Homepage.class);
        startActivity(intent);
        finish();
    }
}
}

Dashboard.xml

<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"
    android:orientation="vertical"
    tools:context=".Dashboard">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/main_toolbar"/>

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:rowCount="3"
            android:columnCount="2"
            android:alignmentMode="alignMargins"
            android:layout_marginVertical="10dp"
            android:gravity="center_vertical"
            >

            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                app:cardElevation="8dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:text="Speed"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/speeddisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Km/h"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="8dp"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:text="RPM"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/rpmdisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="revs"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="8dp"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:text="Engine Load"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/loaddisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:text="%"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="8dp"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Coolant Temperature"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/coolantdisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:text="°C"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>


            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="8dp"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:text="Fuel Pressure"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/pressuredisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:text="kPa"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="8dp"
                android:layout_marginTop="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                app:cardBackgroundColor="@color/colorSecondary_Dark"
                android:layout_gravity="fill"
                >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Intake Air Temperature"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:id="@+id/intakedisplay"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="-"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                    <TextView
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:text="°C"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        android:textStyle="bold"/>

                </LinearLayout>
            </com.google.android.material.card.MaterialCardView>
        </GridLayout>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:layout_marginTop="-30dp"
            android:alpha="0.15"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            android:src="@drawable/loginpage"
            >
        </ImageView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorSecondary_Dark">

        <include
            layout="@layout/nav_drawer"/>

    </RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

Dashboard.java

public class Dashboard extends AppCompatActivity {
TextView speed,rpm,test;
int i,j;
Random rand = new Random();

DrawerLayout drawerLayout;
TextView navheader;
ImageView navimage;


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

    drawerLayout = findViewById(R.id.drawer_layout);


    navheader = findViewById(R.id.nav_headertitle);
    navimage = findViewById(R.id.nav_headerpic);

    navheader.setText("Dashboard");
    navimage.setImageResource(R.drawable.dashboardheader);

    createThread();
}

protected void onStart() {
    super.onStart();
    //check if user is logged in
    checkCurrenUser();
}

protected void onPause() {
    super.onPause();
    Homepage.closedrawer(drawerLayout);
}

//simulate data update from rpm and speed
//need to make actual OBD connection
private void createThread(){
    speed = (TextView)findViewById(R.id.speeddisplay);
    rpm = (TextView)findViewById(R.id.rpmdisplay);

    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                do {
                    Thread.sleep(500);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            i = rand.nextInt(160);
                            j= rand.nextInt(6000);
                            speed.setText(String.valueOf(i));
                            rpm.setText(String.valueOf(j));
                        }
                    });
                }while(true);
            } catch (InterruptedException e) {
            }
        }
    };
    thread.start();
}
public void ClickDrawer(View view){
    Homepage.opendrawer(drawerLayout);
}

public void ClickLogo(View view){
    Homepage.closedrawer(drawerLayout);
}

public void ClickReset(View view){
    Homepage.redirectActivity(this,Resetpassword.class);
}

public void ClickAboutUs(View view){
    //display about page
    setContentView(R.layout.activity_scanpage);
}

public void ClickLogout(View view){
    //create an alert dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Logout");
    builder.setMessage("Confirm Logout?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            logout();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    builder.show();
}

public void logout(){
    Intent logout = new Intent(this, Login.class);
    Toast.makeText(this,"Username: "+ FirebaseAuth.getInstance().getCurrentUser().getDisplayName()+ "\nLogout Successful",Toast.LENGTH_SHORT).show();
    FirebaseAuth.getInstance().signOut();
    logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(logout);
    finish();
}

public void checkCurrenUser() {
    FirebaseUser fuser = FirebaseAuth.getInstance().getCurrentUser();
    if(fuser !=null){
        TextView displayname = findViewById(R.id.displayname_textview);
        String name = fuser.getDisplayName();
        displayname.setText("Welcome! "+ name);
    }
    else{
        Toast.makeText(getApplicationContext(), "No User Logged In", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Homepage.class);
        startActivity(intent);
        finish();
    }
}

Nav_drawer.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="140dp"
    android:adjustViewBounds="true"
    android:alpha="0.02"
    android:backgroundTintMode="add"
    android:scaleType="centerCrop"
    android:src="@drawable/logintop">
</ImageView>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_marginTop="-140dp"
    android:onClick="ClickLogo"
    android:src="@drawable/titlehead"
    >
</ImageView>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-20dp"
    android:text="Remote Vehicle Diagnostic System"
    android:shadowColor="@color/font_Secondary"
    android:textSize="20sp"
    android:textStyle="bold"
    android:textAlignment="center"
    >
</TextView>

<TextView
    android:id="@+id/displayname_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="15sp"
    android:textStyle="bold"
    android:textAlignment="center"
    android:layout_marginTop="15dp"
    android:background="@color/font_Secondary"
    >
</TextView>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:background="@color/colorPrimary_Dark"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:onClick="ClickReset">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Reset Password"
        android:padding="12dp"
        android:layout_marginStart="16dp"
        >
    </TextView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/ic_resetpassword">
    </ImageView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:onClick="ClickAboutUs">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="About Us"
        android:padding="12dp"
        android:layout_marginStart="16dp"
        >
    </TextView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/ic_info">
    </ImageView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:onClick="ClickLogout"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Logout"
        android:padding="12dp"
        android:layout_marginStart="16dp"
        >
    </TextView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:src="@drawable/ic_logout">
    </ImageView>
</LinearLayout>
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56

2 Answers2

0

You need to create a BaseActivity which will include your Navigation Drawer and other widgets which you want to share with other screens. Then you need to extend all the other activities with BaseActivity. You can access drawer without any crashes.

Priyanka
  • 1,791
  • 1
  • 7
  • 12
0

This is not how you create a navigation drawer.

You create a base activity which will have other screens. Those screens won't be activities, but Fragments.

  • Create a fragment_container in the base activity layout xml file using FrameLayout
  • Then create those screens you want to show i.e. create Fragments
  • Then link those Fragments into the base activity.

Here is link for navigation drawer tutorial which I referred to when I was learning about navigation drawer: https://guides.codepath.com/android/fragment-navigation-drawer

Simran Sharma
  • 852
  • 7
  • 16