【发布时间】:2022-12-18 07:22:05
【问题描述】:
我在导航组件中使用底部导航视图,出了点问题 我尝试了很多来解决这个问题,但仍然崩溃,有人可以告诉我缺少什么吗
错误:
ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.runze.stathelper/com.runze.stathelper.activity.MainActivity}: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #9 in com.runze.stathelper:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context androidx.fragment.app.FragmentHostCallback.getContext()' on a null object reference
这是我的导航 xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_main"
app:startDestination="@id/fragment_home">
<fragment
android:id="@+id/fragment_home"
android:name="com.runze.stathelper.fragment.HomeFragment"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/fragment_edit"
android:name="com.runze.stathelper.fragment.EditFragment"
android:label="@string/button_nav_2"
tools:layout="@layout/fragment_edit" />
</navigation>
这是主要活动 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_main" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/button_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
主要活动:
package com.runze.stathelper.activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.runze.stathelper.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.fragmentContainerView)
bottomNavigationView.setupWithNavController(navController)
}
}
虚拟和物理 android 12 设备均出现错误
感谢您帮助修复错误
【问题讨论】:
-
super.onCreate(savedInstanceState)应该是方法中的第一行。永远不要为系统 API 改变它。它在 super 调用上进行初始化。 -
我已经把它放在第一行,但仍然出现错误
-
这不是解决问题的最佳实践。这是启动器 Activity 吗?如果不添加代码,您将如何启动它。您可能处于调试模式,但您在测试时是否有机会启用 minify?或者您可能缺少 dependency。
标签: android kotlin navigation