【问题标题】:Error inflating class androidx.fragment.app.FragmentContainerView, other answers were tried but still not work膨胀类 androidx.fragment.app.FragmentContainerView 时出错,尝试了其他答案但仍然无效
【发布时间】: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


【解决方案1】:

尝试切换到view binding(不推荐使用synthetic)并像这样更改您的代码:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navController = findNavController(R.id.fragmentContainerView)
        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
    }
}

【讨论】:

    【解决方案2】:

    有同样的问题。开发人员的指南确实对我有用

     val navHostFragment =
        supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController = navHostFragment.navController
    findViewById<BottomNavigationView>(R.id.bottom_nav)
        .setupWithNavController(navController)
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      相关资源
      最近更新 更多