0

I'm using bottom navigation view with navigation component, something went wrong I tried a lot to solve the issue but still crash, can someone tell me what's missing

error:

ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.runze.stathelper/com.runze.stathelper.activity.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
       
     Caused by: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context androidx.fragment.app.FragmentHostCallback.getContext()' on a null object reference

here's my navigation xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_main"
    app:startDestination="@id/fragment_home">

    <fragment
        android:id="@+id/fragment_home"
        android:name="com.runze.stathelper.fragment.HomeFragment"
        tools:layout="@layout/fragment_home" />


    <fragment
        android:id="@+id/fragment_edit"
        android:name="com.runze.stathelper.fragment.EditFragment"
        android:label="@string/button_nav_2"
        tools:layout="@layout/fragment_edit" />


</navigation>

and here is main activity xml:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_main" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:menu="@menu/button_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

main activity:

package com.runze.stathelper.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.runze.stathelper.R
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navController = findNavController(R.id.fragmentContainerView)
        bottomNavigationView.setupWithNavController(navController)
        

    }
}

error occurred on both virtual and physical android 12 devices

thanks for help fix the error

Runze Lee
  • 11
  • 1
  • `super.onCreate(savedInstanceState)` should be the first line inside the method . never change that for system APIs . it does initialization stuff on super call. – ADM Mar 31 '22 at 06:35
  • i have put it to the first line and error still occurred – Runze Lee Mar 31 '22 at 06:39
  • That wasn't suppose to fix issue its the best practice . Is this the launcher Activity ? if not add the code how you are starting it . You are probably in debug mode but by any chance u have minify enable while you are testing this ? Or might possible you are missing the [dependency](https://stackoverflow.com/a/60009930/4168607). – ADM Mar 31 '22 at 06:59

2 Answers2

0

Try to switch to view binding (synthetic is deprecated) and change your code like this:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navController = findNavController(R.id.fragmentContainerView)
        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
    }
}
lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
0

Had same problem. Guide from devs did work for

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

findViewById<BottomNavigationView>(R.id.bottom_nav)
    .setupWithNavController(navController)

``
G. Ciardini
  • 1,210
  • 1
  • 17
  • 32
SirBzik
  • 1
  • 1
  • 1