1

Descripción del problema:

Tengo una actividad con un botón. Este botón abre otra actividad la cual contiene un fragment. Por algun extraño motivo al tocar el botón, la app. crashea.

Código JAVA de la actividad que contiene el fragment:

public class categorias extends AppCompatActivity {

    private Fragment fragment_categorias;

    private FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categorias);

        fragment_categorias = new fragment_musica();

        ft.replace(R.id.fragment_categorias,fragment_categorias).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).addToBackStack(null).commit();

    }
}

Código XML de la actividad que contiene el fragment:

<?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="com.example.ui.INICIO.RESULTADOS.CATEGORIAS.categorias"
android:contentDescription="@string/ayuda">

<fragment
    android:id="@+id/fragment_categorias"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/et_buscar"
    android:contentDescription="@string/ayuda"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Adjunto el error que me lanza Android Studio:

Caused by: android.view.InflateException: Binary XML file line #71: Binary XML file line #71: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #71: Error inflating class fragment Caused by: java.lang.NullPointerException

1 respuesta 1

1

Con respecto a el error, en tu Fragment estas haciendo referencia a un elemento con id et_buscar el cual no existe en en layout:

<fragment
    android:id="@+id/fragment_categorias"
    ...
    ...
    app:layout_constraintTop_toBottomOf="@+id/et_buscar"
    .../>

Debes considerar algo importante:

Si deseas realizar un "reemplazo" de un Fragment, solo puede reemplazar un Fragment agregado dinámicamente". Los Fragments definidos en un layout no pueden ser reemplazados.

Por lo tanto en lugar de un Fragment:

<fragment
    android:id="@+id/fragment_categorias"
    ...
    ...
   />

usa un FrameLayout en el cual no defines un Fragment, pero si puedes realizar reemplazos de Fragmentos dentro de este elemento:

<FrameLayout
    android:id="@+id/fragment_categorias"
    ...
    ...
   />

1
  • 1
    Mereces el premio al mejor Jorge. Gracias nuevamente!
    – Tomas M
    el 30 ene. 2020 a las 12:15

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.