Even after I had ensured that the toolbar id is well linked in the main activity where I declared the Toolbar, it still does push an error.
I am trying to create and understand a navigation drawer in android studio. I have once done this navigation and it worked, just trying to build a project using navigation and it started showing this error.
Here are my codes:
This is the main activity:
package com.example.demoapp;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar.findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
}
This is the toolbar layout.xml for the tool bar:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:id="@+id/tool_bar"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
/>
</LinearLayout>
This is the main activity xml where I included all the fragments of xml i needed to make the navigation view work
<?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"
android:id="@+id/drawer"
tools:context=".MainActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/drawer_toolbar"
/>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/content_main"
/>
<com.google.android.material.navigation.NavigationView
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="@+id/navigationView"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header"
android:fitsSystemWindows="true"
android:layout_gravity="start"
/>
</androidx.drawerlayout.widget.DrawerLayout>