使用findFragmentById的ClassCastException

时间:2011-11-18 10:44:58

标签: android-fragments

我尝试使用以下内容获取特定片段:

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

但是我收到了来自eclipse的错误消息:

  

无法从Fragment转换为RohwareFragmentMatlist

活动以:

开头
public class RohwareActionBar extends FragmentActivity {...

RohwareFragmentMatlist定义如下:

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

Fragment以这种方式定义:

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

使用ListFragment通过“getFragmentById”获取片段不起作用吗?

1 个答案:

答案 0 :(得分:33)

我找到了解决方案here。 由于我使用兼容包v4,我必须使用

  

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

而不是

  

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

相关问题