0

I am trying to make my Bottom Navigation Bar clickable so that it actually redirects me to the correct page, however the app doesn't even start. It crashes before I can even click anything. The thing is, I don't even know what the error is. The debugger is telling me that the following line has something wrong with it:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.frameLayout) as NavHostFragment

So this is the exact error I'm getting:

java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

This is my Kotlin code (MainActivity.kt):


import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_main.*


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


        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.frameLayout) as NavHostFragment

        val navController = navHostFragment.navController

        bottomNavigationView.setupWithNavController(navController)

        recyclerView_main.setBackgroundColor(Color.WHITE)
        recyclerView_main.layoutManager = LinearLayoutManager(this)
        recyclerView_main.adapter = MainAdapter()

    }
}

Here's the [updated after seeing replies] code for the Fragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/frameLayout"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".apply">

    <Button
        android:id="@+id/button"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="100dp"
        android:text="@string/apply"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="175dp"
        android:layout_marginEnd="182dp"
        android:layout_marginBottom="100dp"
        android:text="@string/total_200"
        android:textColor="#000000"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="177dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="177dp"
        android:text="Application Menu"
        android:textColor="#000000"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Zaed
  • 21
  • 5
  • https://stackoverflow.com/questions/61827683/null-navhostfragment-navcontroller-with-fragmentcontainerview Check this answer its similar to your problem – Danish Feb 07 '21 at 05:23

2 Answers2

0

The NavHostFragment must be in an activity. Remove the id and name attributes from your fragment and put them in the main activity layout.

// activity_main.xml
<androidx.fragment.app.FragmentContainerView
    android:id="@+id/frameLayout"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

// fragment.xml
<androidx.constraintlayout.widget.ConstraintLayout 
    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">
    ...
</androidx.constraintlayout.widget.ConstraintLayout>
Sinner of the System
  • 2,558
  • 1
  • 8
  • 17
0

did you add the name attribute?

<androidx.fragment.app.FragmentContainerView
android:id="@+id/frameLayout"
android:name="androidx.navigation.fragment.NavHostFragment"
YZN
  • 133
  • 1
  • 8
  • I tried this just now, but my app crashed right after starting :( – Zaed Feb 07 '21 at 20:25
  • 2
    post your layout XML code, your class code, and the error message ( I can check it if you wish) – YZN Feb 08 '21 at 05:57
  • 2
    check this, it might help [link] (https://stackoverflow.com/questions/61827683/null-navhostfragment-navcontroller-with-fragmentcontainerview) – YZN Feb 08 '21 at 05:59