I get this error when I am trying to replace the parent fragment (SearchFragment
) from the child fragment (ClassroomsTab
) in a swipe tab viewPager
. Basically what I am trying to achieve is have a swipe tab menu with multiple buttons in each child fragment that closes the swipe tab menu and open a different existing fragment. The SearchFragment
is in a Navigation Drawer activity and so is the destination fragment of the buttons.
From what I have read, a solution to this issue is to replace the getChildFragmentManager()
in this line of code: viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
with getActivity().getSupportFragmentManager()
or getFragmentManager()
. This solves the issue of the app crashing, but when I click back on the search tab through the navigation drawer from the new fragment, most of the swipe tab child fragments with the buttons don't show up.
Thanks for the help :)
SearchFragment
OnCreateView
code:
public TabLayout tabLayout;
public ViewPager viewPager;
public static int int_items = 5;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this inflates the tab layout file.
View view = inflater.inflate(R.layout.search_layout,null);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
// this puts the titles on the top
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
//this preloads the other tabs. currently only have it set to one for testing
viewPager.setOffscreenPageLimit(1);
return view;
}
ClassroomsTab
code (with only one button for now while I'm testing):
note: content_frame
is the framelayout of the navigation activity
public class ClassroomsTab extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.classrooms, container, false);
defineButtons();
return myView;
}
public void defineButtons() {
Button CR1C1 = (Button)myView.findViewById(R.id.CR1C1);
CR1C1.setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentmanager = getFragmentManager();
switch (view.getId()) {
case R.id.CR1C1:
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
,new MapFragment())
.commit();
break;
}
}
};
Full error
09-08 15:52:13.154 12220-12220/com.example.jeremy.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jeremy.app, PID: 12220
java.lang.IllegalArgumentException: No view found for id 0x7f0800c4 (com.example.jeremy.app:id/content_frame) for fragment MapFragment{c7d968f #2 id=0x7f0800c4}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1417)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2333)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2240)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…