Ad

I'm Receiving Caused By: Android.view.InflateException: Binary XML File Line #32: Error Inflating Class Androidx.fragment.app.FragmentContainerView

So I was trying to get my fragments with navigation component to work, but for some reason I'm getting that exception. I already searched for some solutions but didnt mine.Don't mind some spanish variables. I'm receiving the exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iesnervion.pmdmo_prac2eva/com.iesnervion.pmdmo_prac2eva.view.MainActivity}: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class androidx.fragment.app.FragmentContainerView

and it says that its caused by:

Caused by: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class androidx.fragment.app.FragmentContainerView

My Fragment:

class TiendaFragment : Fragment(), SearchView.OnQueryTextListener {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private lateinit var adapter: ProductoAdapter
private var listaProductos = mutableListOf<ProductoEntidad>()
private var _binding: FragmentTiendaBinding? = null
private val binding get() = _binding!!
private val productoViewModel: ProductoViewModel by activityViewModels()
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        param1 = it.getString(ARG_PARAM1)
        param2 = it.getString(ARG_PARAM2)
    }

    val database = Room.databaseBuilder(requireContext(), ProductoDatabase::class.java, "tienda-db").fallbackToDestructiveMigration().build()

    val productos = ProductoService().getAllProductos()
    lifecycleScope.launch {
        productos.forEach { database.getDao().insertProductos(it) }
    }

    binding.svProductos.setOnQueryTextListener(this)
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentTiendaBinding.inflate(inflater, container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    navController = findNavController()
    inicializarRecyclerView()
}

fun inicializarRecyclerView(){
    adapter = ProductoAdapter(listaProductos){ onProductoSeleccionado(it) }
    binding.rvProductos.layoutManager = LinearLayoutManager(requireContext())
    binding.rvProductos.adapter = adapter

    productoViewModel.getAllProductos()
    val productos = productoViewModel.productoModel
    listaProductos.removeAll(listaProductos)
    listaProductos.addAll(productos)
    adapter.notifyDataSetChanged()
}

private fun onProductoSeleccionado(producto: ProductoEntidad){
    productoViewModel.productoSeleccionado.postValue(producto)
    navController.navigate(R.id.action_tiendaFragment_to_detallesFragment)
}

private fun searchByName(query: String){
    CoroutineScope(Dispatchers.IO).launch {
        val listaProductosLlamada = ProductoService().getAllProductos()
        if(!listaProductosLlamada.isNullOrEmpty()){
            listaProductos.clear()
            listaProductos.addAll(listaProductosLlamada)
            adapter.notifyDataSetChanged()
        }else{
            showError()
        }
        hideKeyboard()
    }
}

/**
 * Oculta el teclado, sin mas, no hay que profundizar mucho en esto
 */
private fun hideKeyboard(){
    val imm = requireActivity().getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(binding.root.windowToken, 0)
}

private fun showError(){
    Toast.makeText(requireContext(), "Ha ocurrido un error", Toast.LENGTH_SHORT).show()
}

override fun onQueryTextSubmit(query: String?): Boolean {
    if(!query.isNullOrEmpty()){ //no uso el query?.let ya que eso solo funciona con nulos, pero no cuando esta vacio
        searchByName(query.lowercase())
    }
    return true
}

override fun onQueryTextChange(newText: String?): Boolean {
    return true //ya que no queremos que pase nada con cada cambio en el texto, solo queremos que se cambie cuando se busque
}

companion object {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TiendaFragment.
     */
    // TODO: Rename and change types and number of parameters
    @JvmStatic
    fun newInstance(param1: String, param2: String) =
        TiendaFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
}

My Fragment XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.TiendaFragment">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.SearchView
        android:id="@+id/svProductos"
        android:layout_width="250dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvProductos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/precioTotalValue" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtPrecioTotal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Precio:"
        android:layout_marginLeft="12dp"
        android:layout_marginBottom="25dp"
        android:textSize="25sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@id/precioTotalValue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/txtPrecioTotal"
        android:layout_marginLeft="12dp"
        tools:text="25€"
        android:layout_marginBottom="25dp"
        android:textSize="25sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

And this is the activity_main.xml (where I'm getting the error):

<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=".view.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/titulo"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:text="@string/shop_name"
    android:layout_marginTop="15dp"
    android:textSize="25sp" />

<androidx.fragment.app.FragmentContainerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/nav_host_fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:layout_constraintTop_toBottomOf="@id/titulo"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"

    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph"
    />

And I don't know if u need this, but this is my nav_graph.xml

<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/tiendaFragment">
<fragment
    android:id="@+id/tiendaFragment"
    android:name="com.iesnervion.pmdmo_prac2eva.fragments.TiendaFragment"
    android:label="fragment_tienda"
    tools:layout="@layout/fragment_tienda" >
    <action
        android:id="@+id/action_tiendaFragment_to_detallesFragment"
        app:destination="@id/detallesFragment" />
</fragment>
<fragment
    android:id="@+id/detallesFragment"
    android:name="com.iesnervion.pmdmo_prac2eva.fragments.DetallesFragment"
    android:label="fragment_detalles"
    tools:layout="@layout/fragment_detalles" />

Thanks for trying to help and if u need some other files in order to find a fix, just comment it

EDIT:

This is the complete stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.iesnervion.pmdmo_prac2eva, PID: 13690
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iesnervion.pmdmo_prac2eva/com.iesnervion.pmdmo_prac2eva.view.MainActivity}: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class androidx.fragment.app.FragmentContainerView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: java.lang.NullPointerException
    at com.iesnervion.pmdmo_prac2eva.fragments.TiendaFragment.getBinding(TiendaFragment.kt:47)
    at com.iesnervion.pmdmo_prac2eva.fragments.TiendaFragment.onCreate(TiendaFragment.kt:65)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2981)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:474)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
    at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2773)
    at androidx.fragment.app.Fragment.onCreate(Fragment.java:1913)
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:264)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2981)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:474)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:257)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1764)
    at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1670)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:323)
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.kt:158)
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:53)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:295)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:274)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)

E/AndroidRuntime:     at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
    at com.iesnervion.pmdmo_prac2eva.view.MainActivity.onCreate(MainActivity.kt:16)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

EDIT 2: I have 2 Fragments in my Activity, here you can see the design view: Navigation Graph Design View

And here is my MainActivity code:

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

}

}

Ad

Answer

onCreate() is too early to access binding that you are only setting up later in onCreateView().

You can e.g. move the binding.svProductos.setOnQueryTextListener(this) to onViewCreated().

Ad
source: stackoverflow.com
Ad