Android 在fragmentTransaction.replace()方法参数中放置什么id?

Android 在fragmentTransaction.replace()方法参数中放置什么id?,android,android-fragments,fragmenttransaction,Android,Android Fragments,Fragmenttransaction,我试图从片段中调用片段 我正在使用以下代码: Fragment fragment = new TeamDetails3(); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame_container, fragment);

我试图从片段中调用片段

我正在使用以下代码:

Fragment fragment = new TeamDetails3();

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frame_container, fragment);
                fragmentTransaction.commit();
我当前的片段布局是
team\u details3.xml
,我调用的片段具有布局
team\u details4.xml
。我也试着把

fragmentTransaction.replace(R.id.team_details4, fragment);
fragmentTransaction.replace(R.id.team_details3, fragment);
但它们显示出错误

如果我放入
fragmentTransaction.replace(R.id.frame\u container,fragment)
-


试试这个。我想应该有用

FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment_container, fragmentB, tag);

    ft.addToBackStack(null);

    ft.commit();

我就是这样做的。希望能有帮助

            //calling the next fragment. i have written it in another class which extends Fragment
            //ContactsFragment is the fragment that I am calling
            ContactsFragment mContactsFragment = new ContactsFragment();
            FragmentTransaction transaction = fragManager.beginTransaction();
            //here default layout is a linear layout in the xml file of this activity  
            transaction.replace(R.id.defaultLayout,new ContactsFragment());
            transaction.commit();
基本上,在
transaction.replace()
中,第一个参数将是要放置片段的布局的id。第二个参数应该是片段。

试试下面的代码

 FragmentTransaction    fragmentTransition = getFragmentManager().beginTransaction();
                        TeamDetails3 fragmentClass = new TeamDetails3 ();
                        fragmentTransition.replace(R.id.realtabcontent, fragmentClass ,"");
                        fragmentTransition.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        fragmentTransition.addToBackStack(fragmentClass .getClass().getName());
                        fragmentTransition.commit();

这是到开发者网站的链接

正如上面提到的,id必须是您需要放置此片段的容器的id。既然您是从android.R.id.content开始的,我猜这就是您需要做的
通过,除非问题中没有包含其他代码。

您只需使用一个布局id即可将片段更改为片段管理器中的布局


您可以使用此id
android.R.id.content
作为更改片段的主布局

@Rod\u Algonquin activity posted所以您只想替换片段?@Rod\u Algonquin是的..我从activity nd调用了片段,这很好,但当我尝试用另一个片段替换tht片段时,它会显示错误,然后将其用作id
android.R.id.content
@Rod\u Algonquin非常感谢!!你能把这个作为答案贴出来吗?这样我就可以选择它作为正确的答案了;如果我将此片段或活动的xml文件布局的id放在红色中,则使用此选项。它显示为无法解析符号。此外,它也无法解析符号R.anim。请从右侧输入\u,从右侧输入R.anim.exit\u_left@Stranger从右侧删除R.anim.enter_我已经为它编写了一个文件,它在您的代码中无法工作。尝试清理您的项目并重新构建它。so transaction.replace()是否包含当前片段或活动的布局id?我当前的片段xml布局是team_details2.xml我尝试放置R.id.team_details2和R.layout.team_details2,但两者都显示错误。第一个显示编译错误,第二个在运行时用框架布局id替换它我应该用当前片段布局id或活动替换它吗带有活动布局的布局idreplace显示与-fragmentTransition相同的错误。替换(R.layout.team_详细信息,fragmentClass,“”);对于片段TeamDetails3{4194d020#1 id=0x7f030052},找不到id为0x7f030052(com.pepup.league:layout/team_details)的视图时出错
 FragmentTransaction    fragmentTransition = getFragmentManager().beginTransaction();
                        TeamDetails3 fragmentClass = new TeamDetails3 ();
                        fragmentTransition.replace(R.id.realtabcontent, fragmentClass ,"");
                        fragmentTransition.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        fragmentTransition.addToBackStack(fragmentClass .getClass().getName());
                        fragmentTransition.commit();