7

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?

whyoz
  • 5,168
  • 47
  • 53
Kay Gladen
  • 1,730
  • 2
  • 14
  • 14

1 Answers1

33

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

Community
  • 1
  • 1
Kay Gladen
  • 1,730
  • 2
  • 14
  • 14
  • Thank you! I was fighting with this for some time and was ready to start a whole new approach. This little tidbit fixed me right up. – Jason Down Oct 15 '12 at 14:36