2011-11-18 12 views
7

que tratar de conseguir un fragmento específico con:ClassCastException con findFragmentById

RohwareFragmentMatlist fragment = (RohwareFragmentMatlist)getFragmentManager().findFragmentById(R.id.lagerfragment); 

pero me da un error de Eclipse con este mensaje:

no puede lanzar desde Fragmento de RohwareFragmentMatlist

La actividad comienza con:

public class RohwareActionBar extends FragmentActivity {... 

El RohwareFragmentMatlist se define de la siguiente manera:

public class RohwareFragmentMatlist extends ListFragment 
     implements LoaderManager.LoaderCallbacks<Cursor>{... 

El fragmento se define de esta manera:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:name="de.ypssoft.RohwareFragmentMatlist" 
android:layout_weight="1" 
android:layout_width="0px" 
android:layout_height="match_parent" 
android:id="@+id/lagerfragment" 
android:layout_margin="5dip" 
android:layout_marginLeft="10dip" 
> 
</fragment> 
<FrameLayout 
android:id="@+id/details" 
android:layout_weight="3" 
android:layout_width="0px" 
android:layout_height="match_parent" 
android:background="?android:attr/detailsElementBackground" /> 

</LinearLayout> 

qué no funciona para obtener un fragmento a través de "getFragmentById" usando ListFragment?

Respuesta

33

Encontré la solución here. Desde que estoy usando paquete de compatibilidad v4 Tengo que usar

RohwareFragmentMatlist fragmento = (RohwareFragmentMatlist) getSupportFragmentManager() findFragmentById (R.id.lagerfragment).;

en lugar de

RohwareFragmentMatlist fragmento = (RohwareFragmentMatlist) getFragmentManager() findFragmentById (R.id.lagerfragment).;

+0

¡Gracias! Estuve peleando con esto por un tiempo y estaba listo para comenzar un nuevo enfoque. Este pequeño bocado me solucionó. –

Cuestiones relacionadas