I am trying to put 2 fragments inside a fragment. I found some code on the Internet, but as far as I could, I was unable to place 2 fragments in 1 fragment. I saw tips regarding the FragmentManager and especially the getChildFragmentManager () method, but I donβt know how it works with 2 fragments.
For the story, I use an action with an ActionBar that creates 3 fragments. In one of them I need to process the chart and a kind of menu to change the scale of the chart. So I need 2 fragments in one fragment.
Here is the code:
A fragment that processes others:
public class GraphDisplayFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View myFragmentView = inflater.inflate(R.layout.graph_fragment, container, false); return myFragmentView; } }
Code for drawing a graph:
public class GraphFragment extends Fragment { private static final int SERIES_NR = 1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { GraphicalView myFragmentView = ChartFactory.getTimeChartView(this.getActivity(), getDateDemoDataset(), getDemoRenderer(),null); return myFragmentView; }
XML files:
graph_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/graph_fragment" android:name="com.test.GraphFragment" android:layout_width="match_parent" android:layout_height="259dp" > </fragment> <fragment android:name="com.test.GraphDetailFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/graph_detail_fragment"> </fragment> </LinearLayout>
graph_detail.xml with test implementation
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="211dp" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
The strange thing is that it works at the beginning when I switch between fragments in an ActionBar, but after 3-4 moves I get this error:
android.view.InflateException: Binary XML file line #7: Error inflating class fragment
If anyone has a solution, it will be awesome!