gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:44:06 30 4
gpt4 key购买 nike

所以我试图让带有导航组件的 fragment 正常工作,但由于某种原因我遇到了异常。我已经搜索了一些解决方案,但不是我的。不要介意一些西类牙变量。我收到异常:

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

它说它是由以下原因引起的:

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

我的 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)
}
}
}

我的 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>

这是 activity_main.xml(我收到错误的地方):

<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"
/>

我不知道你是否需要这个,但这是我的 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" />

感谢您的帮助,如果您需要其他文件以找到修复程序,请发表评论

编辑:

这是完整的堆栈跟踪:

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)

编辑 2:我的 Activity 中有 2 个 fragment ,在这里您可以看到设计 View : Navigation Graph Design View

这是我的 MainActivity 代码:

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

}

最佳答案

onCreate() 访问 binding 还为时过早,您稍后才在 onCreateView() 中设置它。

例如,您可以将 binding.svProductos.setOnQueryTextListener(this) 移动到 onViewCreated()

关于android - 我收到 Caused by : android. view.InflateException : Binary XML file line #32: Error inflating class androidx. fragment.app.FragmentContainerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71154255/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com