首页 > 解决方案 > 尝试使用自定义工具栏和导航组件设置后退按钮时出现空错误

问题描述

这是我第一次使用导航组件,我正在努力使用我的自定义工具栏进行设置。我可以很好地在视图之间导航,但现在我想在内部视图上显示一个后退按钮。但是,我不断收到Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference错误,不知道我做错了什么。

MainActivity.kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  
    setSupportActionBar(findViewById(R.id.toolbar))

    val navController = findNavController(this, R.id.nav_host_fragment)

    NavigationUI.setupActionBarWithNavController(this, navController) // NPE happening here
    NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host_fragment))

}

}

activity_main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbarHolder"
        layout="@layout/snippet_toolbar_plain" />

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph" />

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

snippet_toolbar_plain.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/yellow"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<TextView
    android:id="@+id/tvToolbarTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Tracker" />

</androidx.appcompat.widget.Toolbar>

非常感谢。

标签: androidkotlintoolbarandroid-toolbarandroid-architecture-navigation

解决方案


当你使用

<include
    android:id="@+id/toolbarHolder"
    layout="@layout/snippet_toolbar_plain" />

android:id您放在这里的 替换元素根元素上的ID layout/snippet_toolbar_plain。因此findViewById(R.id.toolbar)正在返回null-您不再有视图命名toolbar,只有现在命名的元素toolbarHolder。这意味着您根本没有设置 ActionBar(您已将其设置为 null),这就是为什么getSupportActionBar()返回 null 而您得到一个NullPointerException.

您可以简单地删除android:id="@+id/toolbarHolder"并且您findViewById(R.id.toolbar)将返回一个非空的工具栏。或者你可以改变你findViewById的使用R.id.toolbarHolder


推荐阅读