【问题标题】:How to get my Bottom Navigation View to work?如何让我的底部导航视图工作?
【发布时间】:2018-09-19 09:51:10
【问题描述】:

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

这是我的活动中的代码:

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"/>

以及来自其他活动的 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" />

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

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

【问题讨论】:

    标签: android xml kotlin bottomnavigationview


    【解决方案1】:

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

    onCreate() 方法内部:

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

    然后:

    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) 
    

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

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

    【讨论】:

    • 非常感谢,它成功了。我很新,并从另一个人那里复制了我使用的代码。我仍然想知道为什么它一直对我的其他两项活动有效,但对这一项却没有。非常感谢
    • 很高兴听到这个消息。请记住不要两次使用相同的 id。我也已经从 Android Studio 中看到过这种奇怪的行为,这可能是因为添加了 Kotlin,有时可能会有奇怪的行为。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      相关资源
      最近更新 更多