Пытаюсь использовать NavHostFragment и получаю сообщение об ошибке: ошибка раздувания класса androidx.fragment.app.FragmentContainerView

#android #kotlin #navigation #nav

#Android #kotlin #навигация #навигация

Вопрос:

вот причина: » Вызвано: android.view.Исключение InflateException: строка двоичного файла XML # 21 в com.example.dnaire:layout / main: строка двоичного файла XML #21 в com.example.dnaire:layout / main: ошибка раздувания класса androidx.fragment.app.FragmentContainerView, вызванная: android.view.Исключение InflateException: строка двоичного XML-файла # 21 в com.example.dnaire:layout /main: Ошибка раздувания класса 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)
    }
}
 

это main.xml файл, содержащий FragmentContainerView, который вызывает проблему:

 <?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>
 

Ответ №1:

Ваша активность должна быть дочерней FragmentActivity , чтобы использовать androidx.fragment.app.FragmentContainerView либо расширение FragmentActivity , либо AppCompatActivity дочерний FragmentActivity класс. Рекомендуется расширить AppCompatActivity .

зависимость от gradle для AppCompat в jectpack implementation 'androidx.appcompat:appcompat:1.2.0'

Ответ №2:

если вы используете jetpack, создайте вместо 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
}}
 

Ответ №3:

В моем случае я просто забыл добавить фрагмент 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">