Android 尝试使用自定义工具栏和导航组件设置后退按钮时出现空错误
这是我第一次使用导航组件,我正努力用我的自定义工具栏设置它。我可以在视图之间导航,但是现在我想在内部视图上显示一个后退按钮。然而,我不断地在一个空对象引用上尝试调用虚拟方法void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)错误,我不确定我做错了什么 MainActivity.ktAndroid 尝试使用自定义工具栏和导航组件设置后退按钮时出现空错误,android,kotlin,toolbar,android-toolbar,android-architecture-navigation,Android,Kotlin,Toolbar,Android Toolbar,Android Architecture Navigation,这是我第一次使用导航组件,我正努力用我的自定义工具栏设置它。我可以在视图之间导航,但是现在我想在内部视图上显示一个后退按钮。然而,我不断地在一个空对象引用上尝试调用虚拟方法void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)错误,我不确定我做错了什么 MainActivity.kt class MainActivity : AppCompatActivity() { override fun onCreate
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>
代码段\u工具栏\u 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\u toolbar\u plain
元素根元素上的id。因此findViewById(R.id.toolbar)
正在返回null
-您不再有名为toolbar
的视图,只有现在名为toolbarHolder
的元素。这意味着您根本没有设置ActionBar(您已将其设置为null),这就是为什么getSupportActionBar()
返回null,而您得到的是NullPointerException
您只需删除android:id=“@+id/toolbarHolder”
,您的findViewById(R.id.toolbar)
将返回非空工具栏。或者,您可以在使用时将您的findViewById
更改为使用R.id.toolbarHolder
<include
android:id="@+id/toolbarHolder"
layout="@layout/snippet_toolbar_plain" />
您放在这里的android:id
将替换layout/snippet\u toolbar\u plain
元素根元素上的id。因此findViewById(R.id.toolbar)
正在返回null
-您不再有名为toolbar
的视图,只有现在名为toolbarHolder
的元素。这意味着您根本没有设置ActionBar(您已将其设置为null),这就是为什么getSupportActionBar()
返回null,而您得到的是NullPointerException
您只需删除
android:id=“@+id/toolbarHolder”
,您的findViewById(R.id.toolbar)
将返回非空工具栏。或者您可以将您的findViewById
更改为使用R.id.toolbarHolder
非常感谢!将findViewById更改为使用R.id.toolbarHolder对我来说并没有真正起作用,但将id从包含的布局中删除成功了,现在我有了
。干杯,非常感谢!将findViewById更改为使用R.id.toolbarHolder对我来说并没有真正起作用,但将id从包含的布局中删除成功了,现在我有了
。干杯