Home > Enterprise >  kotlin java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.frag
kotlin java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.frag

Time:03-16

I looked at past questions but i can't still solve my problem I am getting this error : java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

My MainActivity code is :

class MainActivity : AppCompatActivity() {




override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController =navHostFragment.navController
    val navBottomView:  BottomNavigationView = findViewById(R.id.bottomNavigation)
    navBottomView.setupWithNavController(navController)

}

} My HomeFragment code is this

class HomeFragment : Fragment() {

private var activity: MainActivity?= null



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    activity = activity
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val  view : View = inflater.inflate(R.layout.fragment_home,container,false)
    return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}

}

ActivityMain.xml code is here too :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">

<FrameLayout
    android:id="@ id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/app_bar_layout" >

    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graoh" />

</FrameLayout>
<com.google.android.material.appbar.AppBarLayout
    android:id="@ id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/dashboard_toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white">
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@ id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            app:menu="@menu/bottom_nav_menu"
            app:labelVisibilityMode="unlabeled"
            android:layout_marginRight="15dp"/>

    </androidx.appcompat.widget.Toolbar>

</com.google.android.material.appbar.AppBarLayout>

I can't run my app. If some one can help me it will be perfect

CodePudding user response:

I have a suggestion that for some reason your nav_host_fragment is not created. Try to look at your gradle, most likely the problem is in your implementation

CodePudding user response:

The problem is in the MainActivity where you do not have setContentView(R.id.activity_main)

extra notes I have noticed in your homeFragment you have: activity = activity // which is self assign

and in your main activity xml you have a typo in the following line: app:navGraph="@navigation/nav_graoh"

and you are missing the closing tag of relative layout

  • Related