Search code examples
androidandroid-fragmentsandroid-dialogfragment

Adding fragment to a dialog fragment


I have a layout which needs fragment based on the value of 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;
}

I believe that the view of the fragment should be added in the onViewCreated as the container for the fragment should be inflated first.

 @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;
    }
}

But I am getting this error:

java.lang.IllegalArgumentException: No view found for id 0x7f0f00a9 
(in.jiyofit.newjiyofit:id/aonboarding_fl_inputs) for fragment TestFragment

I put the getFragmentManager inside a postDelayed thread. The activity_onboarding layout is getting inflated fine. But still I am getting the error. This is the xml for TestFragment:

<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 of the parent layout

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

Solution

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

    Change your above lines with following lines.

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

    NOTES :

    When u are adding or replacing fragment from activity then u can directly use your activity fragment manager i.e getFragmentManager but when u need to add / replace fragment from fragment layout then referring to getFragmentManager() will make code to find from xml of your parent activity instead of fragment xml. So it will not find any view referring to your provided id there and app will crash.

    You need to use getChildFragmentManager() to refer current fragment.