java.lang.IllegalArgumentException:如果没有查看发现ID 0x1020002(机器人:ID /内容)的片段 [英] java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment

查看:604
本文介绍了java.lang.IllegalArgumentException:如果没有查看发现ID 0x1020002(机器人:ID /内容)的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动从一个片段到另一台。它显示了在片段事务过程中的错误 -

  java.lang.IllegalArgumentException:如果没有查看发现ID 0x1020002(机器人:ID /内容)的片段PhotosFragment2 {41a57218#3 ID = 0x1020002}
            在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
            在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
            在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
            在android.support.v4.app.FragmentManagerImpl $ 1.运行(FragmentManager.java:446)
            在android.os.Handler.handleCallback(Handler.java:615)
            在android.os.Handler.dispatchMessage(Handler.java:92)
            在android.os.Looper.loop(Looper.java:153)
            在android.app.ActivityThread.main(ActivityThread.java:5086)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:511)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:821)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            在dalvik.system.NativeStart.main(本机方法)
 

下面是classes.I已经使用下面的code的片段交易

 片段片段=新PhotosFragment2();

                        FragmentTransaction fragmentTransaction = getFragmentManager()的BeginTransaction()。
                        fragmentTransaction.replace(android.R.id.content,片段);
                        fragmentTransaction.commit();
 

PhotosFragment.java

 公共类PhotosFragment扩展片段{
        私人FragmentActivity myContext;
        @覆盖
        公共无效onAttach(活动活动){
            myContext =(FragmentActivity)的活动;
            super.onAttach(活动);
        }

        @覆盖
        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                                 捆绑savedInstanceState){

            查看rootView = inflater.inflate(R.layout.photos,集装箱,假);

            rootView.setVerticalScrollBarEnabled(假);
            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适配器=新CustomGridSingle2(myContext,mThumbIds);
            GridView的网格=(GridView控件)rootView.findViewById(R.id.gridView);
            最后ImageView的IMG =(ImageView的)rootView.findViewById(R.id.imageView7);
            grid.setFocusable(假);

            grid.setAdapter(适配器);
    grid.setOnItemClickListener(新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
            片段片段=新PhotosFragment2();

            FragmentTransaction fragmentTransaction = getFragmentManager()的BeginTransaction()。
            fragmentTransaction.replace(android.R.id.content,片段);
            fragmentTransaction.commit();
        }
    });
            CustomGridSingle2适配器2 =新CustomGridSingle2(myContext,mThumbIds2);
            GridView控件GRID2 =(GridView控件)rootView.findViewById(R.id.gridView2);
            grid2.setFocusable(假);
            grid2.setAdapter(适配器2);
            grid2.setOnItemClickListener(新AdapterView.OnItemClickListener(){
                @覆盖
                公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                    片段片段=新PhotosFragment2();

                    FragmentTransaction fragmentTransaction = getFragmentManager()的BeginTransaction()。
                    fragmentTransaction.replace(android.R.id.content,片段);
                    fragmentTransaction.commit();
                }
            });

            返回rootView;
        }


    }
 

PhotosFragment2.java

 公共类PhotosFragment2扩展片段{
    私人FragmentActivity myContext;
    @覆盖
    公共无效onAttach(活动活动){
        myContext =(FragmentActivity)的活动;
        super.onAttach(活动);
    }
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        查看rootView = inflater.inflate(R.layout.photos2,集装箱,假);
        myContext.getActionBar()隐藏()。
        返回rootView;
    }
}
 

活动的xml文件

 < android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>


    <的FrameLayout
        机器人:ID =@ + ID / frame_container
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

    <的LinearLayout
        机器人:ID =@ + ID / left_drawer_view
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        机器人:背景=@可绘制/ bgmenu
        机器人:方向=垂直>

        < RelativeLayout的
            机器人:ID =@ + ID / profilelayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0
            机器人:paddingTop =10dp>


            < ImageView的
                机器人:ID =@ + ID / drawer_profile_image
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignBottom =@ + ID / drawer_profile_background
                机器人:layout_alignLeft =@ + ID / drawer_profile_background
                机器人:layout_alignRight =@ + ID / drawer_profile_background
                机器人:layout_alignTop =@ + ID / drawer_profile_background
                机器人:layout_marginBottom =7.667dp
                机器人:layout_marginLeft =6.5dp
                机器人:layout_marginRight =8.3dp
                机器人:layout_marginTop =7.667dp
                机器人:scaleType =centerCrop>< / ImageView的>


            < ImageView的
                机器人:ID =@ + ID / drawer_profile_background
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_centerHorizo​​ntal =真
                机器人:scaleType =centerCrop
                机器人:SRC =@可绘制/ profileblock>

            < / ImageView的>

            < ImageView的
                机器人:ID =@ + ID / settingicon
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_alignBottom =@ + ID / drawer_profile_background
                机器人:layout_marginLeft = -  15dp
                机器人:layout_toRightOf =@ + ID / drawer_profile_background
                机器人:背景=@可绘制/设置/>

            <的TextView
                机器人:ID =@ + ID /用户名
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_below =@ + ID / drawer_profile_background
                机器人:layout_centerHorizo​​ntal =真
                机器人:layout_marginTop =5DP
                机器人:文本=名称
                机器人:textAppearance =机器人:ATTR / textAppearanceListItemSmall
                机器人:文字颜色=@机器人:彩色/白/>


        < / RelativeLayout的>


        <的ListView
            机器人:ID =@ + ID / list_slidermenu
            风格=@风格/按钮样式
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_marginTop =30dp
            机器人:layout_weight =2
            机器人:cacheColorHint =@机器人:彩色/透明
            机器人:choiceMode =singleChoice
            机器人:分隔=@机器人:彩色/透明
            机器人:dividerHeight =1DP
            机器人:listSelector =@机器人:彩色/透明
            机器人:滚动条=无/>

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

解决方案

在使用 fragmentTransaction.replace(R.id.container,片段)它将删除片段那些已经在容器和您的新添加到同一容器中。

现在,我可以建议你2 things.First,如果你想使用 fragmentTransaction.replace(android.R.id.content,片段); ,你在做什么现在,再没有为你的的活动内容使用的setContentView 。这应该能正常运行then.To知道是什么正是 android.R.id.content 是可以参照的这个计算器问答

还是其次,在你的活动的布局有一个的FrameLayout id为内容。然后用

  fragmentTransaction.replace(R.id.content,片段);
fragmentTransaction.addToBackStack(空); //该事务添加到背堆栈,以便用户可以导航回
//提交事务
fragmentTransaction.commit();
 

希望这有助于。

详细信息:

从您的意见看来,您有在得到使用的想法有些问题的FrameLayout 活动的(不是任何的片段布局的布局)。从的Documents

  

的FrameLayout被设计来阻挡的区域中的屏幕上显示单个项目。一般情况下,的FrameLayout应该用于保存单个子视图,因为它可以是难以组织子视图的方式,是扩展到不同的屏幕尺寸没有这些孩子互相重叠。

所以的FrameLayout 的主要目的是阻止以适应最大的子视图所需的面积。如果您使用的FrameLayout 片段容器可以确保您始终有可用的空间来容纳最大的片段的布局。

所以,你可以有你的的FrameLayout 这样的事情在你的活动的布局xml文件

 <的FrameLayout
    机器人:ID =@ + ID /内容
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent>
 <! - 你可以把你当前XML的现有意见在这里,所以是整个XML是现在这个的FrameLayout内 - >
< /的FrameLayout>
 

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$1.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

   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

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

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

解决方案

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.

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 Activity using setContentView.This should work fine then.To know what exactly android.R.id.content is you can refer this stackoverflow question and answer

Or Secondly, In the layout of your Activity have a FrameLayout whose id is content. And then use

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 FrameLayout in your Activity's layout(Not of any of the Fragment's layout).From the Documents

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.

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

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

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

这篇关于java.lang.IllegalArgumentException:如果没有查看发现ID 0x1020002(机器人:ID /内容)的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆