我有一个片段,它包含的XML是这样的:删除所有子片段
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="230dp">
<fragment
android:id="@+id/myFragmen1"
android:name="com...widgets.myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text""/>
</RelativeLayout>
我需要在我的XML添加4个不同的片段。父母片段我开始像这样:
getFragmentManager().beginTransaction()
.replace(R.id.frameContainer, myFragment1)
.addToBackStack(null)
.commit();
所以,一切正常,直到我试图显示片段超过1次。如果我点击返回,并试图再次打开片段,然后我得到这些错误:
Caused by: java.lang.IllegalArgumentException: Binary XML file line #17: Duplicate id 0x7f080130, tag null, or parent id 0xffffffff with another fragment for com...widgets.myViewPager
我知道的问题,也有一个解决方案。我可以删除onDestroy中的片段。但是,我不想这样做! 包含其他片段的xml是动态的。我通过这个包得到布局,所以在这个片段中,我不知道显示了哪个片段。如果我使用这部分超过一次,我必须确定,名称是一样的。所以我的问题是:有没有可能删除每个片段?所以每一个片段都会使自己和父母无关呢?
感谢您的帮助
编辑:
MyFragment myFragment1 = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("layout",R.layout.my_fragment);
myFragment1.setArguments(bundle);
getFragmentManager().beginTransaction()
.replace(R.id.frameContainer, myFragment1)
.addToBackStack(null)
.commit();
这些电话,我从不同的地方做。对于每个调用,我可以用不同的片段创建自己的xml。 在我父片段我这样做:
int layoutId = this.getArguments().getInt("layout", 0);
if(layoutId==0)
throw new RuntimeException("LayoutId not found");
return inflater.inflate(layoutId,container,false);
这样做之后,我的片段加载XML与所有的碎片,这是我在XML设定。所以我可以从外部定义视图。从这个片段中摧毁并重新打开之后,它会抛出错误。这是因为我的xml中的所有片段都没有被销毁!我的父母片段不知道,哪些片段在xml中,所以我想,他们在课堂上摧毁自己。
你在哪里与此达成? –