简体   繁体   中英

Android - Nesting Fragments No View Found

I have three Fragments and they are arranged as shown below:

"Frag A and Frag C is not connected, but Frag B and C are. The each fragment contains 1 listview so every time I triggered an event it will go to the next fragment."

Fragment A ---> (Inside Frag A )Fragment B ---> (Inside Frag B )Fragment C

  • Fragment A(main.xml)
  • Fragment B(R.id.contentFragment)
  • Fragment C(R.id.contentFragment)

I can display Fragment A and Fragment B and is okay, but if I call Fragment C from B I got "No view found in contentFragment=0x7f040039" .

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"  
                android:background="@color/white" 
                android:id="@+id/frameLayout"    
                >

            <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" 
            >
            </ListView>

    <FrameLayout 
    android:id="@+id/contentFragment"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" >
     </FrameLayout>


    </FrameLayout>

</FrameLayout>

FragmentA.java

Inside Fragment A to open Fragment B

 Fragment fragment1 = new FragmentB();

        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.contentFragment, fragment1, fragMainGroups );
        transaction.addToBackStack(fragMainGroups);
        transaction.commit(); 

FragmentB.java

Inside Fragment B to open Fragment C

     String fragGroups = "groups";

    Fragment fragment1 = new FragmentC();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.replace(R.id.contentFragment, fragment1, fragGroups );
    transaction.addToBackStack(fragGroups);
    transaction.commit(); 

FragmentC.java

Inside Fragment C

   public class FragmentItems extends ListFragment{

    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
            }

            public void onCreate(Bundle e)
            {
                super.onCreate(e);
            }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.load_items_activity, container, false);

                    return rootView;
                }
}

Logcat

07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:

07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:
07-25 18:10:20.718: E/AndroidRuntime(24703): FATAL EXCEPTION: main
07-25 18:10:20.718: E/AndroidRuntime(24703): java.lang.IllegalArgumentException: No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-25 18:10:20.718: E/AndroidRuntime(24703):    at android.os.Handler.handleCallback(Handler.java:615)

As your error states you are looking for a view with id contentFragmentTtems inside Fragment B, main view for which is contentFragment and not the whole main.xml . So if you want to add new fragment you should do it also from Fragment A.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"  
                android:background="@color/white" 
                android:id="@+id/frameLayout"    
                >

            <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" 
            >
            </ListView>

    <FrameLayout 
    android:id="@+id/contentFragmentb"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" >
     </FrameLayout>
<FrameLayout 
    android:id="@+id/contentFragmentc"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" >
     </FrameLayout>

    </FrameLayout>

</FrameLayout>

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM