Trying to use NavHostFragment and im getting an error: Error inflating class androidx.fragment.app.FragmentContainerView
Trying to use NavHostFragment and im getting an error: Error inflating class androidx.fragment.app.FragmentContainerView
这是由以下原因引起的:
" 由以下原因引起:android.view.InflateException:com.example.dnaire 中的二进制 XML 文件第 21 行:layout/main:com.example.dnaire 中的二进制 XML 文件第 21 行: layout/main:错误膨胀 class androidx.fragment.app.FragmentContainerView
由以下原因引起:android.view.InflateException:com.example.dnaire 中的二进制 XML 文件第 21 行:layout/main:膨胀 class androidx.fragment.app.FragmentContainerView 时出错
原因:java.lang.reflect.InvocationTargetException"
我的项目代码还不多,刚刚开始。
这是主要内容 activity:
class Main : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mainBinding = MainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
}
}
这是包含导致问题的 FragmentContainerView 的 main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
那是 nav_graph.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/nav_graph"
app:startDestination="@id/startFragment">
<fragment
android:id="@+id/startFragment"
android:name="com.example.dnaire.login.StartFragment"
android:label="fragment_start"
tools:layout="@layout/fragment_start" />
</navigation>
您的活动必须是 FragmentActivity
的子项才能使用 androidx.fragment.app.FragmentContainerView
您可以扩展 FragmentActivity
或 AppCompatActivity
的子项 FragmentActivity
class。建议延长 AppCompatActivity
.
gradle jectpack 中 AppCompat 的依赖性 implementation 'androidx.appcompat:appcompat:1.2.0'
就我而言,我只是忘记在导航文件中添加 startDestination 片段:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_graph"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/nav_main">
如果您使用 Jetpack Compose 而不是 ComponentActivity 到 AppCompatActivity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// compose view code
}}
至
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//compose view code
}}
这是由以下原因引起的: " 由以下原因引起:android.view.InflateException:com.example.dnaire 中的二进制 XML 文件第 21 行:layout/main:com.example.dnaire 中的二进制 XML 文件第 21 行: layout/main:错误膨胀 class androidx.fragment.app.FragmentContainerView 由以下原因引起:android.view.InflateException:com.example.dnaire 中的二进制 XML 文件第 21 行:layout/main:膨胀 class androidx.fragment.app.FragmentContainerView 时出错 原因:java.lang.reflect.InvocationTargetException"
我的项目代码还不多,刚刚开始。
这是主要内容 activity:
class Main : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mainBinding = MainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
}
}
这是包含导致问题的 FragmentContainerView 的 main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
那是 nav_graph.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/nav_graph"
app:startDestination="@id/startFragment">
<fragment
android:id="@+id/startFragment"
android:name="com.example.dnaire.login.StartFragment"
android:label="fragment_start"
tools:layout="@layout/fragment_start" />
</navigation>
您的活动必须是 FragmentActivity
的子项才能使用 androidx.fragment.app.FragmentContainerView
您可以扩展 FragmentActivity
或 AppCompatActivity
的子项 FragmentActivity
class。建议延长 AppCompatActivity
.
gradle jectpack 中 AppCompat 的依赖性 implementation 'androidx.appcompat:appcompat:1.2.0'
就我而言,我只是忘记在导航文件中添加 startDestination 片段:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_graph"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/nav_main">
如果您使用 Jetpack Compose 而不是 ComponentActivity 到 AppCompatActivity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// compose view code
}}
至
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//compose view code
}}