尝试使用自定义工具栏和导航组件设置后退按钮时出现空错误
Null error when attempting to set back button using custom toolbar and navigation components
这是我第一次使用导航组件,我在使用自定义工具栏进行设置时遇到了一些困难。我可以很好地在视图之间导航,但现在我想在内部视图上显示一个后退按钮。但是,我不断收到 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>
非常感谢。
当你使用
<include
android:id="@+id/toolbarHolder"
layout="@layout/snippet_toolbar_plain" />
您在此处放置的 android:id
替换了 layout/snippet_toolbar_plain
元素的根元素上的 ID。因此 findViewById(R.id.toolbar)
是 returning null
- 您不再有名为 toolbar
的视图,只有现在名为 toolbarHolder
的元素。这意味着您根本没有设置 ActionBar(您已将其设置为 null),这就是 getSupportActionBar()
为 returning null 而您得到 NullPointerException
的原因。
您只需删除 android:id="@+id/toolbarHolder"
,您的 findViewById(R.id.toolbar)
将 return 成为一个非空工具栏。或者您可以将 findViewById
更改为使用 R.id.toolbarHolder
.
这是我第一次使用导航组件,我在使用自定义工具栏进行设置时遇到了一些困难。我可以很好地在视图之间导航,但现在我想在内部视图上显示一个后退按钮。但是,我不断收到 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>
非常感谢。
当你使用
<include
android:id="@+id/toolbarHolder"
layout="@layout/snippet_toolbar_plain" />
您在此处放置的 android:id
替换了 layout/snippet_toolbar_plain
元素的根元素上的 ID。因此 findViewById(R.id.toolbar)
是 returning null
- 您不再有名为 toolbar
的视图,只有现在名为 toolbarHolder
的元素。这意味着您根本没有设置 ActionBar(您已将其设置为 null),这就是 getSupportActionBar()
为 returning null 而您得到 NullPointerException
的原因。
您只需删除 android:id="@+id/toolbarHolder"
,您的 findViewById(R.id.toolbar)
将 return 成为一个非空工具栏。或者您可以将 findViewById
更改为使用 R.id.toolbarHolder
.