bottomnav FindViewById get null (kotlin)

FaithNJ :

I am trying to implement bottom navigation with an Activity and use Kotlin. So I search in youtube and I seen a lot of content which uses Fragment for bottom navigation T_T

So I try to copy code from https://www.youtube.com/watch?v=JjfSjMs0ImQ because they use Activity not Fragment

But the problem is they use Java. So I try to convert the code to Kotlin. And then this happened (Logcat)

Caused by: java.lang.IllegalStateException: bottomNavigationView must not be null at com.example.smscandroid.Profile.onCreate(Profile.kt:18)

So I look in Profile.kt

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

    //init
    val bottomNavigationView =
        findViewById<BottomNavigationView>(R.id.bottom_navigation)
    //Set
    bottomNavigationView.selectedItemId = R.id.profile
    //Perform ItemSelectedListener
    bottomNavigationView.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener { menuItem ->
        when (menuItem.itemId) {
            R.id.medication -> {
                startActivity(
                    Intent(
                        applicationContext
                        , Medication::class.java
                    )
                )
                overridePendingTransition(0, 0)
                return@OnNavigationItemSelectedListener true
            }
            R.id.home -> {
                startActivity(
                    Intent(
                        applicationContext
                        , MainActivity::class.java
                    )
                )
                overridePendingTransition(0, 0)
                return@OnNavigationItemSelectedListener true
            }
            R.id.profile -> return@OnNavigationItemSelectedListener true
        }
        false
    })
}


I think the error is at

//init
val bottomNavigationView =
    findViewById<BottomNavigationView>(R.id.bottom_navigation)
//Set
bottomNavigationView.selectedItemId = R.id.profile

I think findViewById<BottomNavigationView>(R.id.bottom_navigation) might get null then bottomNavigationView.selectedItemId = R.id.profile would cause an Exception

This is bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/medication"
        android:title="medication"
        android:icon="@drawable/ic_medication"/>
    <item
        android:id="@+id/home"
        android:title="home"
        android:icon="@drawable/ic_home"/>
    <item
        android:id="@+id/profile"
        android:title="profile"
        android:icon="@drawable/ic_profile"/>
</menu>

Why does findViewById<BottomNavigationView>(R.id.bottom_navigation) get null?

If I write something wrong, tell me, I will improve it.

Luca Murra :

You should have a file named activity_profile.xml in your res/layout that looks like this:

<SomeLayout
   ...
   >

    <BottomNavigationView
        android:id="@+id/this_is_the_id_you_should_use"
        .../>

</SomeLayout>

And then in your Profile.kt you should use

val bottomNavigationView = findViewById<BottomNavigationView>(R.id.this_is_the_id_you_should_use)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415169&siteId=1