找不到ID为0x7f0800c4的片段的视图

时间:2018-09-08 04:22:02

标签: android android-fragments android-viewpager child-fragment

当我尝试从滑动标签SearchFragment的子片段(ClassroomsTab)替换父片段(viewPager)时收到此错误。基本上,我想要实现的是在每个子片段中都有一个带有多个按钮的滑动选项卡菜单,以关闭滑动选项卡菜单并打开一个不同的现有片段。 SearchFragment处于“导航抽屉”活动中,按钮的目标片段也是如此。

据我所读,解决此问题的方法是用getChildFragmentManager()viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));替换以下代码行中的getActivity().getSupportFragmentManager()getFragmentManager()。这解决了应用程序崩溃的问题,但是当我从新片段中通过导航抽屉在搜索选项卡上单击回来时,大多数带有按钮的滑动选项卡子片段都不会显示。

感谢您的帮助:)

SearchFragment OnCreateView代码:

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代码(我正在测试时,现在只有一个按钮):

注意:content_frame是导航活动的框架布局

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;
        }
    }
};

完全错误

  

09-08 15:52:13.154 12220-12220 / com.example.jeremy.app E / AndroidRuntime:致命异常:main       流程:com.example.jeremy.app,PID:12220       java.lang.IllegalArgumentException:找不到片段MapFragment {c7d968f#2 id = 0x7f0800c4}的ID 0x7f0800c4(com.example.jeremy.app:id/content_frame)的视图           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1417)           在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754)           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822)           在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)           在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591)           在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378)           在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2333)           在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2240)           在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:703)           在android.os.Handler.handleCallback(Handler.java:790)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6494)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1 个答案:

答案 0 :(得分:1)

通常会在多个事务中更改布局时发生。 同样的事情发生在我身上,我通过清除它后面的堆栈来解决它。但您必须对所有交易使用一个FragmentManager。 完成交易后放置此代码

    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();
            }
        }
    }
}