getSupportFragmentManager()和getChildFragmentManager()有什么区别?

我的类inheritanceFragment,这就是为什么它不能使用getSupportFragmentManager()。 我正在使用getChildFragmentManager,它显示我错误 – IllegalArguementException:没有发现id …错误的视图。

任何指导将不胜感激。

调用AttachmentsListFragment的代码是

Bundle b = new Bundle(); b.putSerializable("AttachmentsList", msg.attachments); AttachmentListFragment listfrag = new AttachmentListFragment(msg.attachments); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(R.id.attachmentslistcontainer, listfrag); transaction.addToBackStack(null); transaction.commit(); 

attachmentslayout.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:id="@+id/attachmentslistcontainer" android:orientation="vertical" > <TextView android:id="@+id/textViewAttachmentHeader" style="@style/Normal.Header.Toolbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/list_separator_background" android:ellipsize="end" android:gravity="center" android:maxLines="2" android:text="@string/attachments_header" android:textColor="#FFFFFFFF" android:textSize="22sp" android:textStyle="bold" android:visibility="visible" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </FrameLayout> 

AttachmentsListFragment.java

 public class AttachmentListFragment extends ListFragment implements IAttachmentsData { ArrayList<Attachments> items = null; Integer cellLayoutID; Integer index; public AttachmentListFragment() { } public AttachmentListFragment(ArrayList<Attachments> items) { this.items = items; Log.i("Logging", "Items size" + items.size()); //$NON-NLS-1$ } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle bundle; if (savedInstanceState != null) { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub //super.onCreateView(inflater, container, savedInstanceState); // setContentView(R.layout.attachmentslayout); View view = inflater.inflate(R.layout.attachmentslayout, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(new AttachmentAdapter( getActivity().getApplicationContext(), R.layout.attachmentslistcellcontent, items)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); index = position; Intent intent = new Intent(); Bundle b = new Bundle(); b.putByteArray("Data", items.get(position).getImageData()); intent.putExtras(b); } public byte[] getData() { // TODO Auto-generated method stub if (items != null && index < items.size()) { return items.get(index).getImageData(); } return null; } } 

getChildFragmentManager()的定义是:

返回一个专用的FragmentManager来放置和pipe理这个Fragment中的Fragments。

同时getFragmentManager() (或者在这个例子中是getSupportFragmentManager() )的定义是:

返回FragmentManager以与与此片段的活动关联的片段进行交互。

基本上不同的是,Fragment现在有自己的内部FragmentManager ,可以处理碎片。 子FragmentManager是仅处理被添加到其中的片段中的碎片的碎片pipe理器。 另一个FragmentManager包含在整个Activity

在这种情况下,我猜测你已经将Fragments添加到Activity的FragmentManager中。 你得到的孩子FragmentManager不包含你在找什么。 因此你会得到这个exception,因为它找不到具有给定ID的Fragment,因为它在不同的FragmentManager中。