【问题标题】:bottomnav FindViewById get null (kotlin)bottomnav FindViewById 获取 null (kotlin)
【发布时间】:2020-01-30 17:57:32
【问题描述】:

我正在尝试使用 Activity 实现底部导航并使用 Kotlin。所以我在 youtube 上搜索,我看到很多使用Fragment 进行底部导航的内容 T_T

所以我尝试从https://www.youtube.com/watch?v=JjfSjMs0ImQ 复制代码,因为他们使用Activity 而不是Fragment

但问题是他们使用 Java。所以我尝试将代码转换为 Kotlin。然后发生了这种情况(Logcat)

原因:java.lang.IllegalStateException:bottomNavigationView 不能为空 在 com.example.smscandroid.Profile.onCreate(Profile.kt:18)

所以我查看 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&lt;BottomNavigationView&gt;(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&lt;BottomNavigationView&gt;(R.id.bottom_navigation) 会得到null

如果我写错了,告诉我,我会改进它。

【问题讨论】:

  • activity_profile.xml 文件是否包含 ID 为 bottom_navigation 的底部导航?跨度>
  • 你能添加你的R.layout.activity_profile xml吗?

标签: java android kotlin bottomnavigationview findviewbyid


【解决方案1】:

您的 res/layout 中应该有一个名为 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多