java.lang.IllegalArgumentException: 没有找到 id 0x1020002 (android:id/content) 的片段视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25665642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:38:13  来源:igfitidea点击:

java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment

androidfragmenttransaction

提问by Android Developer

I am trying to move from one fragment to another.. It shows following error during fragment transaction-

我正在尝试从一个片段移动到另一个片段。它在片段事务期间显示以下错误-

    java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment PhotosFragment2{41a57218 #3 id=0x1020002}
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
            at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:446)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)

Below are the classes.I have used following code for fragment transaction

以下是类。我使用以下代码进行片段交易

Fragment fragment = new PhotosFragment2();

                        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                        fragmentTransaction.replace(android.R.id.content, fragment);
                        fragmentTransaction.commit();

PhotosFragment.java

照片片段.java

   public class PhotosFragment extends Fragment {
        private FragmentActivity myContext;
        @Override
        public void onAttach(Activity activity) {
            myContext = (FragmentActivity) activity;
            super.onAttach(activity);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.photos, container, false);

            rootView.setVerticalScrollBarEnabled(false);
            int[] mThumbIds = {
                    R.drawable.album8, R.drawable.album3,
                    R.drawable.album4, R.drawable.album8,
                    R.drawable.album6, R.drawable.album7,
                    R.drawable.album12, R.drawable.album10,
            };

            int[] mThumbIds2 = {
                    R.drawable.album8, R.drawable.album3,
                    R.drawable.album4,
                    R.drawable.album6, R.drawable.album7,
                    R.drawable.album9, R.drawable.album10,
                    R.drawable.album11, R.drawable.album12, R.drawable.album8,
                    R.drawable.album8, R.drawable.album3,
                    R.drawable.album4,
                    R.drawable.album6, R.drawable.album7,
                    R.drawable.album9, R.drawable.album10,
                    R.drawable.album11, R.drawable.album12, R.drawable.album8,
            };

            CustomGridSingle2 adapter = new CustomGridSingle2(myContext, mThumbIds);
            GridView grid = (GridView)rootView.findViewById(R.id.gridView);
            final ImageView img= (ImageView)rootView.findViewById(R.id.imageView7);
            grid.setFocusable(false);

            grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fragment fragment = new PhotosFragment2();

            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.replace(android.R.id.content, fragment);
            fragmentTransaction.commit();
        }
    });
            CustomGridSingle2 adapter2 = new CustomGridSingle2(myContext, mThumbIds2);
            GridView grid2 = (GridView)rootView.findViewById(R.id.gridView2);
            grid2.setFocusable(false);
            grid2.setAdapter(adapter2);
            grid2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Fragment fragment = new PhotosFragment2();

                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(android.R.id.content, fragment);
                    fragmentTransaction.commit();
                }
            });

            return rootView;
        }


    }

PhotosFragment2.java

照片片段2.java

public class PhotosFragment2 extends Fragment {
    private FragmentActivity myContext;
    @Override
    public void onAttach(Activity activity) {
        myContext = (FragmentActivity) activity;
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.photos2, container, false);
        myContext.getActionBar().hide();
        return rootView;
    }
}

Activity xml file

活动xml文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/left_drawer_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/bgmenu"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/profilelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:paddingTop="10dp">


            <ImageView
                android:id="@+id/drawer_profile_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/drawer_profile_background"
                android:layout_alignLeft="@+id/drawer_profile_background"
                android:layout_alignRight="@+id/drawer_profile_background"
                android:layout_alignTop="@+id/drawer_profile_background"
                android:layout_marginBottom="7.667dp"
                android:layout_marginLeft="6.5dp"
                android:layout_marginRight="8.3dp"
                android:layout_marginTop="7.667dp"
                android:scaleType="centerCrop"></ImageView>


            <ImageView
                android:id="@+id/drawer_profile_background"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:scaleType="centerCrop"
                android:src="@drawable/profileblock">

            </ImageView>

            <ImageView
                android:id="@+id/settingicon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/drawer_profile_background"
                android:layout_marginLeft="-15dp"
                android:layout_toRightOf="@+id/drawer_profile_background"
                android:background="@drawable/settings" />

            <textview
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/drawer_profile_background"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceListItemSmall"
                android:textColor="@android:color/white" />


        </RelativeLayout>


        <ListView
            android:id="@+id/list_slidermenu"
            style="@style/buttonStyle"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="30dp"
            android:layout_weight="2"
            android:cacheColorHint="@android:color/transparent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="1dp"
            android:listSelector="@android:color/transparent"
            android:scrollbars="none" />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

采纳答案by Sash_KP

When you use fragmentTransaction.replace(R.id.container,fragment)it will remove any fragments that are already in the container and add your new one to the same container.

当您使用fragmentTransaction.replace(R.id.container,fragment)它时,它将删除容器中已有的所有片段,并将新的片段添加到同一个容器中。

Now i can suggest you 2 things.First, if you want to use fragmentTransaction.replace(android.R.id.content, fragment);which you are doing right now,then don't set content for your Activityusing setContentView.This should work fine then.To know what exactly android.R.id.contentis you can refer this stackoverflow question and answer

现在我可以建议你两件事。首先,如果你想使用fragmentTransaction.replace(android.R.id.content, fragment);你现在正在做的事情,那么不要为你的Activity使用设置内容setContentView。这应该可以正常工作。要知道到底android.R.id.content是什么,你可以参考这个 stackoverflow 问题和回答

Or Secondly, In the layout of your Activityhave a FrameLayoutwhose id is content. And then use

或者其次,在你的布局中Activity有一个FrameLayoutid 是content. 然后使用

fragmentTransaction.replace(R.id.content, fragment);  
fragmentTransaction.addToBackStack(null);//add the transaction to the back stack so the user can navigate back
// Commit the transaction
fragmentTransaction.commit();  

Hope this helps.

希望这可以帮助。

More Info:

更多信息:

From your comments it seems that you are having some problem in getting the idea of using FrameLayoutin your Activity's layout(Not of any of the Fragment's layout).From the Documents

从您的评论看来,您在获得FrameLayout在您Activity的布局中使用的想法时遇到了一些问题(不是任何Fragment的布局)。来自文档

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

FrameLayout 旨在在屏幕上遮挡一个区域以显示单个项目。通常,FrameLayout 应该用于保存单个子视图,因为很难以可缩放到不同屏幕尺寸的方式组织子视图而不会使子视图相互重叠。

So the main purpose of FrameLayoutis to block the area required to fit the largest child view. If you use a FrameLayoutas FragmentContainer you can ensure that you always have the space available to accommodate the largest Fragment's layout.

所以主要目的FrameLayout是阻挡适合最大子视图所需的区域。如果您使用 aFrameLayout作为FragmentContainer,您可以确保始终有可用空间来容纳最大Fragment的布局。

So you can have your FrameLayoutsomething like this in your Activity's layout xml file

所以你可以FrameLayout在你Activity的布局xml文件中有这样的东西

<FrameLayout
    android:id="@+id/content"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
 <!--you can put your existing views of your current xml here, so yes your entire xml is now inside this FrameLayout -->
</FrameLayout>

回答by NhatVM

I don't use getSupportFragmentManager(). I use getChildFragmentManager()and it worked for me. You can try it.

我不使用getSupportFragmentManager(). 我使用getChildFragmentManager()它,它对我有用。你可以试试看。

回答by Andrei Tr.

If you are trying to put Fragmentinto Fragment, use getChildFragmentManager()

如果你正试图把Fragment进入Fragment,使用getChildFragmentManager()

回答by John Cordeiro

You have to set a content view in your Activity that your PhotosFragment is attached.

您必须在您的 Activity 中设置一个内容视图,您的 PhotosFragment 已附加。

protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);

    //...
}

回答by georgehardcore

If you are using ViewPager, use getChildFragmentManager() for pager adapter

如果您使用的是 ViewPager,请使用 getChildFragmentManager() 作为寻呼机适配器

viewPager.setAdapter(new ItemsAdapter(getChildFragmentManager()));

回答by Summved Jain

There are 2 most common reasons for this problem

此问题有 2 个最常见的原因

  1. Specifying the wrong layout in setContentView() of the onCreate() method
  2. Error also occurs when having nested Fragments and adding them with getSupportFragmentManager() instead of getChildFragmentManager()
  1. 在 onCreate() 方法的 setContentView() 中指定错误的布局
  2. 嵌套 Fragment 并使用 getSupportFragmentManager() 而不是 getChildFragmentManager() 添加它们时也会发生错误

Hope this will help

希望这会有所帮助

Summved

总和

回答by AKber

Since Android 4.2 (API 17)nested fragments become avaliable. See this

由于Android 4.2 (API 17)嵌套片段变得可用。看到这个

To place fragment inside other fragment use getChildFragmentManager()

将片段放在其他片段中使用 getChildFragmentManager()

Also available in support library.

也可在支持库中使用。

回答by Bahu

i know this is very late. But it may work for other's.

我知道这已经很晚了。但它可能适用于其他人。

I declared the id for each layout, like (3rd line bellow)

我为每个布局声明了 id,例如(下面的第 3 行)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"

回答by Bruce too

The problem may be that you add a fragment and beginTransaction in some fragment like

问题可能是您在某些片段中添加了一个片段和 beginTransaction,例如

getFragmentManager().beginTransaction().replace(android.R.id.content, ImageBrowseFragment.newInstance(bundle),"ImageBrowseFragment")
            .addToBackStack(null).commit();

I just change getFragmentManager()to getActivity().getSupportFragmentManager()

我只是getFragmentManager()改为getActivity().getSupportFragmentManager()

Then everything goes fine

然后一切顺利

When you add a fragment in a fragment use android.R.id.contentOr Window.ID_ANDROID_CONTENT,I guess it represents the root container of your fragment not the activity. So change to getActivity().getSupportFragmentManager()works it out

当您在片段中添加片段时使用android.R.id.contentOr Window.ID_ANDROID_CONTENT,我猜它代表片段的根容器而不是活动。所以改变来解决getActivity().getSupportFragmentManager()

回答by pavithra

I had the same error and after trying a lot of things I found that the problem was with the "id" and "getActivity().getSupportFragmentManager()".

我遇到了同样的错误,在尝试了很多事情之后,我发现问题出在“id”和“getActivity().getSupportFragmentManager()”上。

Before i implement i like these

在我实施之前,我喜欢这些

txt_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    getChildFragmentManager()
                            .beginTransaction()
                            .replace(R.id.content_frame, new OpportunitiesFragment())
                            .commit();                }

            });

Then i changed to these

然后我改成了这些

 txt_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
    ## Heading ##
                        getActivity().getSupportFragmentManager()
                                .beginTransaction()
                                .replace(android.R.id.content, new OpportunitiesFragment())
                                .commit();                }
                });