【问题标题】:Trying to use NavHostFragment and im getting an error: Error inflating class androidx.fragment.app.FragmentContainerView尝试使用 NavHostFragment 时出现错误:Error inflating class androidx.fragment.app.FragmentContainerView
【发布时间】:2021-01-11 17:29:06
【问题描述】:

这是由以下原因引起的: ” 引起:android.view.InflateException:com.example.dnaire:layout/main 中的二进制 XML 文件第 21 行:com.example.dnaire:layout/main 中的二进制 XML 文件第 21 行:膨胀类 androidx.fragment 时出错.app.FragmentContainerView 引起:android.view.InflateException:com.example.dnaire:layout/main 中的二进制 XML 文件第 21 行:错误膨胀类 androidx.fragment.app.FragmentContainerView 引起:java.lang.reflect.InvocationTargetException"

我在项目中还没有太多代码,刚刚开始。

这里是主要活动:

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>

【问题讨论】:

    标签: android kotlin navigation nav


    【解决方案1】:

    您的活动必须是 FragmentActivity 的子级才能使用 androidx.fragment.app.FragmentContainerView 或者扩展 FragmentActivityAppCompatActivity 子级 FragmentActivity。建议扩展AppCompatActivity

    jectpack implementation 'androidx.appcompat:appcompat:1.2.0' 中 AppCompat 的 gradle 依赖

    【讨论】:

      【解决方案2】:

      就我而言,我只是忘记在导航文件中添加 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">
      

      【讨论】:

        【解决方案3】:

        如果您使用 jetpack compose 而不是 ComponentActivityAppCompatActivity

        class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }}
        

         class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-27
          • 1970-01-01
          • 2020-05-17
          相关资源
          最近更新 更多