3

I am trying to call fragment from fragment.

I am using following code:

Fragment fragment = new TeamDetails3();

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

My current fragment layout is team_details3.xml and the fragment which i am calling has layout team_details4.xml. I also tried putting

fragmentTransaction.replace(R.id.team_details4, fragment);
fragmentTransaction.replace(R.id.team_details3, fragment);

but they show error

I am getting these errors if i put fragmentTransaction.replace(R.id.frame_container, fragment)-

No view found for id 0x7f080159 (com.pepup.league:id/frame_container) for fragment TeamDetails3{41916708 #1 id=0x7f080159}
            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)
08-22 12:07:49.420      347-363/? E/AppErrorDialog﹕ Failed to get ILowStorageHandle instance
Rami
  • 7,879
  • 12
  • 36
  • 66

5 Answers5

3

You only need to use one layout id for changing fragment as the layout in fragment managers

You can use this id android.R.id.content as the main layout for changing the fragments

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

Try this. I guess it should work

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

    ft.addToBackStack(null);

    ft.commit();
Akshay Mukadam
  • 2,388
  • 1
  • 27
  • 40
0

This is how I do it. Hope it helps

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

Basically in transaction.replace() the first parameter will be the id of the layout in which you want to place your fragment. And the second parameter should be the fragment.

Rami
  • 7,879
  • 12
  • 36
  • 66
ik024
  • 3,566
  • 7
  • 38
  • 61
  • if i put the id of xml file layout of this fragment or activity it shows in red cannot resolve symbol.. –  Aug 22 '14 at 07:15
  • also it cannot resolve symbol R.anim.enter_from_right,R.anim.exit_to_left –  Aug 22 '14 at 07:15
  • @Stranger remove the R.anim.enter_fromright I have written a file for that it wont work in your code. Try cleaning your project and building it again – ik024 Aug 22 '14 at 07:17
  • okay..so transaction.replace() contans id of layout of current fragment or activity? –  Aug 22 '14 at 07:19
  • my current fragment xml layout is team_details2.xml i tried putting R.id.team_details2 and R.layout.team_details2 but both shows error..first one shows compiling error and second one while running –  Aug 22 '14 at 07:20
  • can u post your xml file i can give u a better solution in that way – ik024 Aug 22 '14 at 07:20
0

try below code

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

This is the link to the developers site. FragmentTransaction.replace

Like it mentions, the id needs to be the id of the container where you need to place this fragment. Since you began with android.R.id.content, I'm guessing that is what you need to pass, unless there is other code that isn't included in the question.

Anudeep Bulla
  • 8,318
  • 4
  • 22
  • 29