2011-11-18 16 views
7

Staram się specyficzny fragment z:ClassCastException z findFragmentById

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

Ale pojawia się błąd z zaćmienia z tej wiadomości:

Nie można przesyłać z fragmentu RohwareFragmentMatlist

Aktywność rozpoczyna się od:

public class RohwareActionBar extends FragmentActivity {... 

RohwareFragmentMatlist jest zdefiniowany następująco:

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

Fragment jest zdefiniowana w ten sposób:

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

to nie działa, aby uzyskać fragment via "getFragmentById" używając ListFragment?

Odpowiedz

33

Znalazłem rozwiązanie here. Ponieważ m użyciu Compatibility V4 trzeba użyć

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

zamiast

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

+0

Dziękujemy! Walczyłem z tym przez jakiś czas i byłem gotowy, aby rozpocząć zupełnie nowe podejście. Ta mała przekąska naprawiła mnie. –

Powiązane problemy