Question

I have a Fragment, which contains two other Fragments, namely a ListFragment and a regular Fragment. When I inflate the parent Fragment I receive an InflateException runtime error.

This is due to like #11 in my XML layout code which refers to this:

<fragment
    android:id="@+id/fragment1"
    android:name="com.nanospark.TMS.fragment_profile_list"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/new_profile_button" />

Keep in mind this XML above refers a nested Fragment.

Is nesting Fragments a supported feature in Android? Are there any alternatives or fixes to this issue?

Logcat:

04-17 15:00:58.198: E/AndroidRuntime(1400): FATAL EXCEPTION: main
04-17 15:00:58.198: E/AndroidRuntime(1400): android.view.InflateException: Binary XML file line #11: Error inflating class fragment

EDIT 1: From what I've discovered it is supported http://developer.android.com/about/versions/android-4.2.html#NestedFragments

However I cannot find any good examples of nested Fragments and filling parent fragments with them.

Était-ce utile?

La solution

You can add it in your fragment code:

public class YourFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.your_fragment_layout, null);

    if (getChildFragmentManager().findFragmentByTag("main") == null)
    {
        if (getActivity() == null)
            return null;
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        NewsMainFragment nf = (YourNested) YourNestedFragementClass.instantiate(getActivity(), YourNestedFragementClass.class.getName(),savedInstanceState);
        ft.add(R.id.fragment_container, nf,"main");
        ft.commit();
    }
    return v;
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top