No view found for id 0x7f0800c4 for fragment

旧街凉风 提交于 2020-03-05 04:41:07

问题


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)


回答1:


This usually happens when you change the layouts in several transactions. the same happened to me and I solved it by clearing the stack behind it. but you have to use a single FragmentManagerfor all of your transactions. put this code after you done the transaction

    private void clearStack() {
    if (fragmentManager.getBackStackEntryCount() > 0) {
        for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
            fragmentManager.popBackStack();
        }
    }

    if (fragmentManager.getFragments().size() > 0) {
        for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
            Fragment mFragment = fragmentManager.getFragments().get(i);
            if (mFragment != null) {
                fragmentManager.beginTransaction().remove(mFragment).commit();
            }
        }
    }
}


来源:https://stackoverflow.com/questions/52231981/no-view-found-for-id-0x7f0800c4-for-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!