FindFragmentById devolver null

Cuando llamo a findFragmentById () con el id de mi fragmento, devuelve null. Déjame mostrarte el código.

El archivo activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.madduck.test.app.fragment.MainFragment" android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:name="com.madduck.test.app.fragment.LoginFragment" android:id="@+id/login_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

El MainActivity.java

 private static final int LOGIN = 0; private static final int MAIN = 1; private static final int FRAGMENT_COUNT = MAIN +1; private Fragment[] fragments = new Fragment[FRAGMENT_COUNT] protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); fragments[LOGIN] = fm.findFragmentById(R.id.login_fragment); fragments[MAIN] = fm.findFragmentById(R.id.main_fragment); FragmentTransaction transaction = fm.beginTransaction(); for (Fragment f : fragments) { if (f != null) transaction.hide(f); else Log.e(TAG, "???"); } transaction.commit(); } 

La cosa es que cuando llamo fm.findFragmentById(R.id.login_fragment); fm.findFragmentById(R.id.main_fragment); null pero cuando llamo fm.findFragmentById(R.id.main_fragment); Tengo el fragmento.

La respuesta Kar0t está perfectamente bien, pero esto puede ayudar a alguien. En mi caso tenía un fragmento dentro de un fragmento y estaba recibiendo el FragmentManager equivocado. Sólo tenía que llamar:

getChildFragmentManager()

Y luego acaba de encontrar el Fragmento como de costumbre:

 fm.findFragmentById(R.id.fragment) 

Acaba de descubrir mi error.

En mi MainActivity.java estaba importando android.support.v4.app.Fragment; Y en mi LoginFragment.java estaba importando android.app.Fragment; . Lo cambié a la misma cosa y fm.findFragmentById(R.id.login_fragment) ahora devuelve el fragmento correcto.

  • NullPointerException acceder a las vistas en onCreate ()
  • NullPointerException intentando acceder a un recurso de cadena
  • Cámara Android: archivo vacío en el método onActivityResult
  • Mi ubicación actual siempre devuelve null. ¿Cómo puedo arreglar esto?
  • Intenta invocar el método virtual 'void android.widget.Button.setOnClickListener (android.view.View $ OnClickListener)' en una referencia de objeto nulo
  • Error invocar método virtual 'doble android.location.Location.getLatitude ()' en una referencia de objeto nulo
  • Aplicaciones se bloquea después de la ofuscación con ProGuard
  • Error de puntero nulo con hideSoftInputFromWindow
  • Intenta invocar el método virtual 'int android.text.Layout.getLineCount ()' en una referencia de objeto nulo
  • Cómo solucionar esto: 'Running android lint' issue
  • NullPointerException al llamar a la clase auto-escrita
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.