当前位置:首页 > 技术心得 > 正文内容

Android error:Duplicate ID, tag null, or parent id with another fragment(fragment嵌套问题)

xjtudll11年前 (2015-10-11)技术心得11060

参考资料:http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi


跟参考资料里的这个人一样,我也碰到了这个问题,原因也与其类似。
我有个Activity,里面有个fragment,而这个fragment对应的xml文件里,又加载了其他fragment。
这是xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_find"
android:layout_gravity="center_horizontal"
android:background="@drawable/find_button_selector"
android:layout_marginBottom="15dip"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="10dip" />

<fragment
android:layout_width="wrap_content"
android:layout_height="0dp"
android:name="com.xonix.notifyWatch.ui.FragmentAlertSetting"
android:id="@+id/fragment_alert"
android:layout_weight="1"
tools:layout="@layout/setting" />

</LinearLayout>
</LinearLayout>

很明显,是属于fragment嵌套。
使用参考资料的方法,先判断view是否存在,不存在则加载xml。
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.find, container, false);
} catch (InflateException e) {
       }
return view;
}
 


扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://xjtudll.cn/Exp/368/

标签: Android
分享给朋友:

“Android error:Duplicate ID, tag null, or parent id with another fragment(fragment嵌套问题)” 的相关文章

ios宏: 判断是否是retina屏,是否iPad,设备判断(4,5,5s,6等)

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #def...

如何在fragment里启动service

参考资料:http://stackoverflow.com/questions/13007355/how-to-start-service-from-fragments 问题: 如果在fragment里直接用startService,那么编译将不会通过。 如下代码在activity里是可以正确使用的...

“CrystalDecisions.ReportSource.ReportSourceFactory”的类型初始值设定项引发异常

“CrystalDecisions.ReportSource.ReportSourceFactory”的类型初始值设定项引发异常

VS2010 System.TypeInitializationException: “CrystalDecisions.ReportSource.ReportSourceFactory”的类型初始值设定项引发异常。 System.TypeInitializationExce...

windows环境下TortoiseSVN多仓库(repository)转移合并(修改+转载)

http://www.lilin.net/blog/?p=1346 问题: 两个不同的版本库,放在不同的及其上,各有数个项目在里面,为了统一管理,我现在都集中在一个仓库内,然后用金山快盘,多个机器同步。 回答: 完美包含版本信息,把双库融合,方便管理同步。 自己尝试后发现问题...

Protel99SE PCB各层的含义

本文收集于网络,整理而成,原文作者未知。 我们在进行印制电路板设计前,第一步就是要选择适用的工作层。Protel 99 SE提供有多种类型的工作层。只有在了解了这些工作层的功能之后,才能准确、可靠地进行印制电路板的设计。 Protel 99 SE所提供的工作层大致可以分为7类:Signal La...

NSToolBar-设定初始化选中的item

NSToolBar-设定初始化选中的item

参考资料: http://stackoverflow.com/questions/11004072/nstoolbar-how-do-i-initialise-with-a-selected-item 使用方法: -setSelectedItemIdentifier 举例如下: [self.win...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。