是getSupportFragmentManager()和getChildFragmentManager()有什么区别? [英] What is difference between getSupportFragmentManager() and getChildFragmentManager()?

查看:163
本文介绍了是getSupportFragmentManager()和getChildFragmentManager()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类继承片段,这就是为什么它不能使用getSupportFragmentManager()。 我使用getChildFragmentManager,它是显示我的错误 - IllegalArguementException:没有查看发现ID ...误差

任何指导,将AP preciated。

code调用AttachmentsListFragment是

 叠B =新包();
b.putSerializable(AttachmentsList,msg.attachments);
        AttachmentListFragment listfrag =新AttachmentListFragment(msg.attachments);
FragmentTransaction交易= getFragmentManager()的BeginTransaction()。
transaction.add(R.id.attachmentslistcontainer,listfrag);
transaction.addToBackStack(空);
器transaction.commit();
 

attachmentslayout.xml是

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / attachmentslistcontainer
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / textViewAttachmentHeader
        风格=@风格/ Normal.Header.Toolbar
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@色/ list_separator_background
        机器人:ellipsize =结束
        机器人:重力=中心
        机器人:MAXLINES =2
        机器人:文本=@字符串/ attachments_header
        机器人:文字颜色=#FFFFFFFF
        机器人:TEXTSIZE =22sp
        机器人:TEXTSTYLE =黑体
        机器人:能见度=看见/>

    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
    < / ListView控件>

< /的FrameLayout>
 

AttachmentsListFragment.java

 公共类AttachmentListFragment扩展ListFragment实现IAttachmentsData {

    ArrayList的<附件>项=无效;
    整数cellLayoutID;
    整数索引;

    公共AttachmentListFragment(){

    }

    公共AttachmentListFragment(ArrayList中<附件>项目){
        this.items =项目;
        Log.i(记录,项目大小+ items.size()); // $ NON-NLS-1 $
    }


    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        捆绑包;
        如果(savedInstanceState!= NULL){
    }


    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        // TODO自动生成方法存根
        //super.onCreateView(inflater,集装箱,savedInstanceState);

        //的setContentView(R.layout.attachmentslayout);
        查看查看= inflater.inflate(R.layout.attachmentslayout,集装箱,假);
        返回查看;
    }


    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        setListAdapter(新AttachmentAdapter(
                getActivity()。getApplicationContext(),
                R.layout.attachmentslistcellcontent,
                项));
    }

    @覆盖
    公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        // TODO自动生成方法存根
        super.onListItemClick(L,V,位置ID);
        指数=位置;
        意向意图=新的意图();
        叠B =新包();
        b.putByteArray(数据,items.get(位置).getImageData());
        intent.putExtras(B);
    }


    公众的byte []的getData(){
        // TODO自动生成方法存根
        如果(项目= NULL和放大器;!&安培;指数< items.size()){

            返回items.get(指数).getImageData();
        }
            返回null;
    }

}
 

解决方案

的定义getChildFragmentManager()是:

  

返回私人FragmentManager用于放置和管理的碎片   这里面片段。

getFragmentManager()(或在这种情况下, getSupportFragmentManager())是

与此同时,定义>

  

返回FragmentManager与相关的片段进行交互   这个片段的活动。​​

基本上,所不同的是片段的,现在有自己的内部的 FragmentManager ,可处理片段。子FragmentManager是处理包含在只知道它被添加到片段的碎片的之一。另FragmentManager包含整个活动之内。

在这种情况下,我猜是你已经添加了片段与活动的FragmentManager。你得到不包含你在找什么对孩子FragmentManager。因此,你得到的异常,因为它无法找到指定的ID的片段,因为它是在不同的FragmentManager。

My class inherits Fragment and that's why it can't use getSupportFragmentManager(). I am using getChildFragmentManager and it is showing me Error - IllegalArguementException: No view found for id... error.

Any guidance would be appreciated.

Code for calling AttachmentsListFragment is

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 is

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

}

解决方案

The definition of getChildFragmentManager() is:

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

Meanwhile the definition of getFragmentManager() (or in this case getSupportFragmentManager()) is:

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.

In this case, what I'm guessing is you've added the Fragments to the Activity's FragmentManager. You get the child FragmentManager which doesn't contain what you are looking for. Thus you get the exception because it can't find the Fragment with the given ID because it's in a different FragmentManager.

这篇关于是getSupportFragmentManager()和getChildFragmentManager()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆