指定的孩子已经有父母

问题描述:

我目前正在两个dialogfragments之间传递数据。每当对话框片段2完成。它将从到对话片段1。这里对于对话片段1的方法传递的数据,实现了在对话片段创建2.指定的孩子已经有父母

对话框片段1

@Override 
public void updateContributor(ArrayList<View> imageView) { 
    System.out.println("Contributors received from contributer's fragment: " + imageView); 
    for (View child : imageView) 
    { 
     if (child instanceof ImageView) { 
      ImageView childImageView = (ImageView) child; 
      ll_contributor_list.addView(childImageView); 
     } 
    } 
} 

对话框片段2的界面

MusicRecorderFragment fragment = (MusicRecorderFragment) getFragmentManager().findFragmentByTag("record_fragment"); 


      ArrayList<View> views = getAllChildren(ll); 

      /*setArguments(args);*/ 
      fragment.updateContributor(views); 
      getDialog().dismiss(); 

java.lang.IllegalStateException:指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。

你会得到这个异常,因为你的视图已经添加了一些布局,你试图再次将它添加到贡献者列表,我猜这是一个布局。 (ll_contributor_list.addView(childImageView);

尝试仅收集数据并在另一个片段上重新创建视图,而不是试图将已添加的视图添加到另一个布局。

+0

是否有另一种方式添加一个项目到已添加视图的布局? – 2014-10-22 08:25:28

+0

如果你使用xml创建视图,那么答案是否定的。如果你动态添加它们,那么你可以使用removeView和addView方法。 – Alex 2014-10-22 08:31:26