Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException with findFragmentById

I try to get a specific Fragment with:

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

But I get an error from eclipse with this message:

Cannot cast from Fragment to RohwareFragmentMatlist

The Activity starts with:

public class RohwareActionBar extends FragmentActivity {...

The RohwareFragmentMatlist is defined as follows:

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

The Fragment is defined this way:

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

Doesn't it work to get a Fragment via "getFragmentById" using ListFragment?

like image 612
Kay Gladen Avatar asked Nov 18 '11 10:11

Kay Gladen


1 Answers

I found the solution here . Since I'm using compatibility package v4 I have to use

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

instead of

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

like image 134
Kay Gladen Avatar answered Nov 22 '22 08:11

Kay Gladen