Error inflating Class fragment in DialogFragment

99封情书 提交于 2019-12-02 09:43:58

But I am getting an Error inflating class fragment when I try to open the fragment dialog and the app crashes

This is happening because you use as the BodyDialogue fragment's view a layout which already includes another fragment(through the fragment tag). This will fail as nested fragments aren't allowed to be inflated from a xml layout, as the guide for the nested fragments already mentions:

Note: You cannot inflate a layout into a fragment when that layout includes a < fragment>. Nested fragments are only supported when added to a fragment dynamically.

So if you want to embed the AndroidXMLParsingActivity(awful naming btw for a fragment) in the BodyDialogue dialog fragment then do it in code in the same onCreateView callback using getChildFragmentManager().

Murtaza Ashraf

I solved my problem using this simple and easy

Please refer to this answer

https://stackoverflow.com/a/14966061/963591

My code using above answer

@Override
public void onClick(View v) {

// TODO Auto-generated method
                                        // stub

FragmentTransaction ft2 = getFragmentManager().beginTransaction();
                                        ft2.remove(getFragmentManager().findFragmentByTag("one"));
                                        ft2.commit();

dialog.dismiss();

}

My xml

<fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="220dip"
            android:tag="one"
            android:layout_below="@+id/txtTargetStreet_hint"
            android:layout_margin="20dip" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!