bottomnav FindViewById GETヌル(kotlin)

FaithNJ:

私はと下部のナビゲーションを実装しようとしているActivityとKotlinを使用しています。私はユーチューブで検索し、私が使用してコンテンツをたくさん見たように、Fragment下のナビゲーションT_Tために

私はからコードをコピーしようとしてhttps://www.youtube.com/watch?v=JjfSjMs0ImQ、彼らが使用しているためActivityではありませんFragment

しかし、問題は、彼らは、Javaを使用しています。だから私はKotlinにコードを変換してみてください。そして、これが起こった(Logcat)

java.lang.IllegalStateException:によって引き起こさbott​​omNavigationViewはcom.example.smscandroid.Profile.onCreate(18 Profile.kt)でヌルであってはなりません

私は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
    })
}


私は、エラーがでていると思います

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

私が考えるfindViewById<BottomNavigationView>(R.id.bottom_navigation)かもしれませんnullその後、bottomNavigationView.selectedItemId = R.id.profile原因となりますException

これは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>

なぜないfindViewById<BottomNavigationView>(R.id.bottom_navigation)取得null

私は何かが間違っを記述する場合、私はそれを改善します、教えてください。

ルカMurra:

あなたはこのようになりますことをご解像度/レイアウト内のファイルの名前activity_profile.xmlを持っている必要があります。

<SomeLayout
   ...
   >

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

</SomeLayout>

そして、あなたのProfile.ktにあなたが使用する必要があります

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=470389&siteId=1