android - 尝试使用自定义工具栏和导航组件设置后退按钮时出现空错误
问题描述
这是我第一次使用导航组件,我正在努力使用我的自定义工具栏进行设置。我可以很好地在视图之间导航,但现在我想在内部视图上显示一个后退按钮。但是,我不断收到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
您放在这里的 替换元素根元素上的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
。
推荐阅读
- ruby-on-rails - 如何根据用户分配的角色限制用户进行编辑和删除操作
- c# - 如何使用 stripe api 更新客户发票详细信息
- python - ImportError:没有名为 grid_search、learning_curve 的模块
- php - 正则表达式 preg_split
- python - 使用条件拆分字符串
- algorithm - 检测连接三角形组
- android - 共享首选项仅在应用重启后更新
- javascript - 目标容器不是使用 webpack 4 和 React 的 DOM 元素
- arm - 将 R0 寄存器总是在其他任何东西之前使用吗?
- c++ - 如何用值替换指令?