There are three pages inherited from Fragment

And in one of them 2 more fragments are created.

public class Info extends TabFragment { private ExpandableListAdapter listAdapter; private ExpandableListView expListView; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; private List<CityInfo> cityinfo; private TabLayout tabLayout; private FragmentActivity mycontext; @Override public void onAttach(Context context) { mycontext = (FragmentActivity) context; super.onAttach(context); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); try{ FragmentManager fm = ((FragmentActivity)view.getContext()).getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.simpleFrameLayout, new ListCity(cityinfo)); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commit(); } catch (Exception e) { e.printStackTrace(); } } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { cityinfo = InternetConnect.getCityInfo(); View view = inflater.inflate(R.layout.maps_city, container, false); FrameLayout _simpleFrameLayout = (FrameLayout) view.findViewById(R.id.simpleFrameLayout); tabLayout = (TabLayout) view.findViewById(R.id.simpleTabLayout); tabLayout.getTabAt(0).setCustomView(R.layout.tab_element_layout); ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.TabMenu)).setText("СПИСОК"); tabLayout.getTabAt(1).setCustomView(R.layout.tab_element_layout); TextView tv = ((TextView) tabLayout.getTabAt(1).getCustomView().findViewById(R.id.TabMenu)); tv.setText("КАРТА"); Typeface tf = Typeface.createFromAsset(mycontext.getAssets(), getString(R.string.digit_keyboard_font)); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { Fragment fragment = null; switch (tab.getPosition()) { case 1: fragment = new MapsActivity(cityinfo); break; case 0: fragment = new ListCity(cityinfo); break; } FragmentManager fm = mycontext.getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.simpleFrameLayout, fragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commit(); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); return view; } 

}

When moving through fragments, an error quickly flies.

 Process: com.example.myapplication, PID: 28641 java.lang.IllegalArgumentException: No view found for id 0x7f100110 (com.example.myapplication:id/simpleFrameLayout) for fragment ListCity{294f7a4f #2 id=0x7f100110} at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293) 

If you remove these lines, the error disappears, but when you move to the desired fragment, it is empty.

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); try{ FragmentManager fm = ((FragmentActivity)view.getContext()).getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.simpleFrameLayout, new ListCity(cityinfo)); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commit(); } catch (Exception e) { e.printStackTrace(); } } 

 <android.support.design.widget.TabLayout android:id="@+id/simpleTabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabBackground="@color/white" app:tabGravity="center" android:theme="@style/TabItem" > <android.support.design.widget.TabItem android:layout_width="match_parent" android:layout_height="match_parent" android:text="СПИСОК" style="@style/TabItem" android:layout_margin="50dp" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="КАРТА" android:padding="50dp" /> </android.support.design.widget.TabLayout> <FrameLayout android:id="@+id/simpleFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> 

  • Show fragment layout with tabs - Yuriy SPb
  • @Yuriy SPb added, everything is standard here - Dezmont
  • Try to remove the animation, i.e. ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); - Yuriy SPb
  • @Yuriy SPb nothing has changed. It seems that onViewCreated is started when the markup is not loaded yet or has already been unloaded from memory. How to make it so that when opening Taba the first fragment is loaded, so to speak, in a different way? - Dezmont

1 answer 1

Judging by the fact that you are trying to place a fragment in onViewCreated , you do this not inside the activation, but inside another fragment. In such cases, it is worth using the Fragment.getChildFragmentManager fragment, and not the FragmentActivity.getSupportFragmentManager activate. True nested fragments are supported only starting from Android 4.2 (API 17), read more here .

Practice shows that it is actually possible to place a fragment inside the View another fragment, it is likely that you will succeed if you do this with some delay after onViewCreated (for example, using Handler.postDelayed ). However, I do not recommend doing so, because in the future you will have problems if you try to remove / replace the container fragment. Using getChildFragmentManager() will not getChildFragmentManager() such problems.

  • one
    Thank. Really fixed changed ((FragmentActivity)view.getContext()).getSupportFragmentManager(); on getChildFragmentManager and added an empty constructor to ListCity and doesn't seem to throw it out yet - Dezmont