尝试使用NavHostFragment,IM收到错误:膨胀类androidx.Fragment.app.FragmentContainerView时出错 [英] Trying to use NavHostFragment and im getting an error: Error inflating class androidx.fragment.app.FragmentContainerView

查看:0
本文介绍了尝试使用NavHostFragment,IM收到错误:膨胀类androidx.Fragment.app.FragmentContainerView时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是由以下原因引起的: 原因:android.view.InflateException:com.example.dnaire:Layout/Main中的二进制XML文件第21行/Main:Layout/Main中的二进制XML文件第21行:Layout/Main:膨胀类androidx.Fragment.app.FragmentContainerView时出错 原因:android.view.InflateException:com.example.dnaire中的第21行二进制XML文件:Layout/Main:膨胀类androidx.碎片化.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>

推荐答案

您的Activity必须是FragmentActivity的子类,才能使用FragmentActivity扩展FragmentActivityAppCompatActivity子类FragmentActivity。建议扩展AppCompatActivity

对象包中AppCompat的Gradle依赖项implementation 'androidx.appcompat:appcompat:1.2.0'

这篇关于尝试使用NavHostFragment,IM收到错误:膨胀类androidx.Fragment.app.FragmentContainerView时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆