如何动态添加片段到现有的片段中?

3

我想在点击一个按钮时将一个片段添加到现有的片段中(按钮位于父片段上),但是出现了错误:java.lang.ClassCastException: 必须实现OnFragmentInteractionListener。

这是什么意思,为什么例子中都没有它?

父片段按钮按下代码:

Button interestedButton = (Button) myFragmentView.findViewById(R.id.interestedButton);
interestedButton.setOnClickListener(new View.OnClickListener() {
    InterestedFormFragment interestedFormFragment = new InterestedFormFragment();
    @Override
    public void onClick(View v) {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.interestedFrag_container, interestedFormFragment, "INTERESTED_FORM");
        fragmentTransaction.commit();
    }
});

父片段XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_gravity="bottom"
android:id="@+id/buildingPageId">

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/interestedFrag_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
...

"interestedFrag_container" 应该容纳新增的子片段。

我的父片段已经被填充,它包含应该添加子片段的按钮。 我在这里缺少什么? 谢谢!


嵌套片段仅在API 17及以上版本中添加。您的目标API是哪个? - Vadim Caen
我认为你已经回答了自己的问题 :) 如果支持的API版本是17及以上,那么我将不得不针对API17或更高版本使用嵌套片段。感谢你的提问。 - KasparTr
如果它解决了你的问题,我可以将其编写为答案 :) - Vadim Caen
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接