Android - 嵌套碎片没有找到视图

时间:2013-07-25 10:36:14

标签: java android android-fragments

我有三个碎片,它们按如下所示排列:

“Frag A和Frag C没有连接,但是Frag B和C都是。每个片段包含1个listview所以每次触发一个事件时它都会转到下一个片段。”

  

片段A ---> (内部片段A)片段B ---> (Inside Frag B)Fragment C

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

我可以显示片段A和片段B并且没关系,如果我从B调用片段C我得到了"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

在片段A内打开片段B

 Fragment fragment1 = new FragmentB();

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

FragmentB.java

在片段B内打开片段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

内部碎片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)

2 个答案:

答案 0 :(得分:0)

正如您的错误所述,您正在片段B中查找ID为contentFragmentTtems的视图,其主视图为contentFragment而不是整个main.xml。因此,如果您想添加新片段,您也应该从片段A中进行。

答案 1 :(得分:0)

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

这应该有用。