将碎片添加到对话框碎片

3
我有一个布局需要根据strInfo的值创建片段。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = getActivity().getLayoutInflater().inflate(R.layout.activity_onboarding, null);
    Bundle bundle = getArguments();
    strInfo = bundle.getString(S.SP_USER_INFO);

    //Lot of code regarding the view

    return view;
}

我认为片段的视图应该在onViewCreated中添加,因为片段的容器应该先被膨胀。

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    int infoCount = 1;
    switch (strInfo){
        case S.UserInfoPrefs.GOAL:
            S.L("AOPOOP");
            infoCount = 1;
            getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();
            break;
        case S.UserInfoPrefs.WEIGHT:
            infoCount = 3;
            S.L("ASFLGH");
            //getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new WeightFragment()).commit();
            break;
    }
}

但是我遇到了这个错误:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00a9 
(in.jiyofit.newjiyofit:id/aonboarding_fl_inputs) for fragment TestFragment

我将getFragmentManager放在postDelayed线程中。activity_onboarding布局被成功地膨胀了。但是我仍然收到错误消息。 这是TestFragment的XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="TEST"
    android:gravity="center"/>

</LinearLayout>

父级布局的XML
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_weight="0.15">

    //some code

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.35"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:gravity="center">

    //some code

</RelativeLayout>

<FrameLayout
    android:id="@+id/aonboarding_fl_inputs"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.3"/>

<Button
    android:id="@+id/aonboarding_btn_proceed"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:text="@string/submit"
    style="@style/ProceedButtonStyle"/>

发布您的DialogFragment的XML。 - rafsanahmad007
activity_onboarding 布局中,您是否有任何 ID 为 aonboarding_fl_inputs 的框架或其他布局? - M D
1个回答

14
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();

请使用以下代码替换您上方的行。

this.getChildFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment());
    t.commit();

注意:

当您在activity中添加或替换fragment时,可以直接使用您的activity fragment manager即getFragmentManager。但是当您需要从fragment布局中添加/替换fragment时,引用getFragmentManager()将会使代码从父activity的xml中查找,而不是从fragment xml中查找。因此,它将无法找到任何与您提供的id相关的视图,并且应用程序将崩溃。

您需要使用getChildFragmentManager()来引用当前fragment。


2
getFragmentManagergetChildFragmentManager有什么区别? - suku
在我的情况下没有帮助。所以我的父片段是一个FragmentModel。有什么区别吗? - Mukarram Ali

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