如何让我的底部导航视图工作?

How to get my Bottom Navigation View to work?

我在我的项目中使用 BottomNavigationView,它工作正常。除了我现在在我的 3 上添加了 BottomNavigationView。Activity 并且只有在那里它抛出异常,我找不到我的代码的问题。

这是我的 Activity:

的代码
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {                                                            //when Bottom Navigationbar is clicked
        R.id.navigation_Auskunft -> { //Different Activities can be opened
            val intent = Intent(this, fahrplanmap::class.java)        //Opens fahrplanmap Class
            startActivity(intent)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_Abfahrtsmonitor -> {
            val intent = Intent(this, Abfahrtsmonitor::class.java)        //Opens fahrplanmap Class
            startActivity(intent)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_Karte -> {
            val intent = Intent(this, MapsAnzeigeActivity::class.java)        //Opens fahrplanmap Class
            startActivity(intent)
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_Meldungen -> {
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_weiteres -> {
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.abfahrtsmonitor)
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) //Here it is red undlerined with the error 
....
}

我的 xml 代码文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     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"
tools:context=".Abfahrtsmonitor">


<EditText
    android:id="@+id/editText"
    android:layout_width="360dp"
    android:layout_height="47dp"
    android:ems="10"
    android:hint="Suche"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="7dp" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/Recycleview"
    android:layout_width="370dp"
    android:layout_height="441dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="4dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="368dp"
    android:layout_height="48dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="511dp"
    android:id="@+id/navigation"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"/>

和来自另一个 activity 的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fahrplanmap">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

在我的其他活动中,我以完全相同的方式定义它们,并且在那里工作完全没有问题。

我上传了一张鼠标悬停错误的图片

可能是因为您从未在 onCreate() 方法中初始化 BottomNavigationView

onCreate() 方法内部:

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

然后:

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) 

此外,不要使用相同的 ID 两次:android:id="@+id/navigation"

这里是使用 BottomNavigationView 的一个很好的示例:https://github.com/hitherejoe/BottomNavigationViewSample/

我认为您的导入语句有问题。

似乎在 abfahrtsmonitor.xmlactivity_fahrplanmap.xml 中都找到了 ID 为 navigation 的元素。由于它们都是导入的 BottomNavigationView 分辨率是不可能的(分辨率不明确)。

如果您需要同时导入 abfahrtsmonitor.xmlactivity_fahrplanmap.xml,您应该在其中一个文件中重命名 ID 为 navigation 的元素。