androidkotlindrawerlayout

DrawerLayout returning null in Kotlin Android application


I'm starting to learn Kotlin for Android apps. This is one of my first projects and I'm trying to set a toggle listener for my drawer menu, but getting an error because it is being returned as null. The ID I'm using to find it seems to be set correctly.

activity_main.xml file:

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

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:title="@string/app_name"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/toolBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The drawer_menu.xml file:

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

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

    <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu"/>
</androidx.drawerlayout.widget.DrawerLayout>

And the MainActivity.kt (where I'm setting the toggle)

class MainActivity : AppCompatActivity() {
    private val rvList: RecyclerView by lazy {
        findViewById<RecyclerView>(R.id.rv_list)
    }

    private val adapter = ContactAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initDrawer()
        bindViews()
        updateList()
    }

    private fun initDrawer() {
        val drawerLayout = findViewById<View>(R.id.drawerLayout) as DrawerLayout
        val toolBar = findViewById<View>(R.id.toolBar) as Toolbar
        setSupportActionBar(toolBar)
        
        val toggle = ActionBarDrawerToggle(this, drawerLayout, toolBar, R.string.open_drawer, R.string.close_drawer)

        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
    }
    ...
}

And this is the error I'm getting:

2022-01-19 10:16:29.863 10567-10567/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: curso.android.contactlist, PID: 10567
    java.lang.RuntimeException: Unable to start activity ComponentInfo{curso.android.contactlist/curso.android.contactlist.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6626)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
     Caused by: java.lang.NullPointerException: null cannot be cast to non-null type androidx.drawerlayout.widget.DrawerLayout
        at curso.android.contactlist.MainActivity.initDrawer(MainActivity.kt:34)
        at curso.android.contactlist.MainActivity.onCreate(MainActivity.kt:27)
        at android.app.Activity.performCreate(Activity.java:7032)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)

Does anyone know why is it returning null?


Solution

  • the drawer_menu isn't inside activity_main but the other way around. so replace

    setContentView(R.layout.activity_main)
    

    with

    setContentView(R.layout.drawer_menu)
    

    There might be more problems but this is what I noticed at first