Home > Net >  Need guidance with bottom_navigation and it
Need guidance with bottom_navigation and it

Time:06-23

Hello I'm new to learning kotlin and trying to make an app as well as following a tutorial. But no matter what video I watch I always get an error for both bottom_navigation and it.

Can anyone have a look at my code and possibly help me figure why theres an error. Thank you for any input :)

Main Activity.ktl

package com.example.myapplication


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.myapplication.Fragments.*
import com.example.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

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

        setContentView(binding.root)

        val homeFragment = HomeFragment()
        val videoFragment = VideoFragment()
        val upcomingFragment = UpcomingFragment()
        val podcastFragment = PodcastFragment()
        val communityFragment = CommunityFragment()

        makeCurrentFragment(homeFragment)

        bottom_navigation.setOnNavigationItemSelectedListener
        when(it.itemId){
                R.id.ic_home_icon_white -> makeCurrentFragment(homeFragment)
                R.id.ic_video_white -> makeCurrentFragment(videoFragment)
                R.id.ic_upcoming_icon -> makeCurrentFragment(upcomingFragment)
                R.id.ic_podcast_white -> makeCurrentFragment(podcastFragment)
                R.id.ic_community_white-> makeCurrentFragment(communityFragment)
            }
        }


    private fun makeCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.fl_wrapper, fragment)
            commit()

  }
}

Main Activity.xml


<RelativeLayout 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:background="#1E1E1E"
    tools:context=".MainActivity">

    <FrameLayout
        android:id = "@ id/fl_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView

        android:id = "@ id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/design_default_color_primary"
        app:itemIconTint="#fff"
        app:itemTextColor="#fff"
        app:menu="@menu/my_nav" />



</RelativeLayout>```

CodePudding user response:

You need to share the error you are facing. but the first line of code missing is initializing the binding to your activity layout:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

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

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.bottomNavigation.setOnNavigationItemSelectedListener{
            when(it.itemId){
                R.id.ic_home_icon_white -> makeCurrentFragment(homeFragment)
                R.id.ic_video_white -> makeCurrentFragment(videoFragment)
                R.id.ic_upcoming_icon -> makeCurrentFragment(upcomingFragment)
                R.id.ic_podcast_white -> makeCurrentFragment(podcastFragment)
                R.id.ic_community_white-> makeCurrentFragment(communityFragment)
            }
        }
        //Rest of your code...
  • Related