找不到用于片段的id 0x7f0800c4的视图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找不到用于片段的id 0x7f0800c4的视图相关的知识,希望对你有一定的参考价值。

当我试图在滑动选项卡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是导航活动的framelayout

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:FATAL EXCEPTION:main进程:com.example.jeremy.app,PID:12220 java.lang.IllegalArgumentException:找不到视图id为0x7f0800c4(com.example.jeremy.app:id/content_frame),用于android.support的android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1417)中的片段MapFragment {c7d968f#2 id = 0x7f0800c4}。位于android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822)的vs.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754),位于android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java: 797)在Android.support.v4.app.FragmentManager上,在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378)的android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591) .removeRedundantOperationsAndExecute(FragmentManager.java:2333)在android.support.v4.app.FragmentManagerImpl.execPendingActions(F ragmentManager.java:2240)在android.os.Handler.dispatchMessage上的android.os.Handler.handleCallback(Handler.java:790)的android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:703)处( Handler.java:99)在android.app.Looper.loop(Looper.java:164)的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)

答案

当您在多个事务中更改布局时,通常会发生这种情况。同样的事发生在我身上,我通过清除它背后的堆栈解决了它。但你必须使用单个FragmentManagerfor你的所有交易。在完成交易后放置此代码

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

以上是关于找不到用于片段的id 0x7f0800c4的视图的主要内容,如果未能解决你的问题,请参考以下文章

Android 片段,在 TabHost 中找不到 id 的视图

Android Fragment找不到ID的视图?

找不到来自非 activity_main.xml 的视图

Kotlin 在片段中找不到按钮 ID,为啥?

Espresso 找不到视图:NoMatchingViewException

添加两个窗格的平板电脑布局会导致在移动设备中找不到视图(小于w600dp)