Dialog的layout中有fragment,第二次报错

1. 概述

Dialog用的是最简单的AlertDialog.Builder建立的,通过setView(view)实现自定义布局,布局里嵌套了一个自定义的fragment。第一次弹出对话框的时候没有问题,第二次再弹的话就会出现错误信息:
Binary XML file line #8: Duplicate id , tag null, or parent id 0xffffffff with another fragment for com.whu.geoname.fragment.ImageFlowFragment。

2. 原因

From stackoverflow:
Does the fragment in your layout have an android:id attribute?

I suspect this is because the fragment is instantiated each time your layout is inflated, the first time the ID isn’t being used, but the second time the FragmentManager still thinks your Fragment is alive, so the ID is considered a duplicate.

3. 解决方法

stackoverflow上提供了三种解决方法:
Try removing the android:id attribute from your fragment if it exists, or add a placeholder layout such as a framelayout and use a fragmenttransaction to dynamically add the fragment each time your dialog is created.
第一种方法我测试了没有产生效果,而且就算可以的话你也不能对你的fragment进行操作,所以我选择了第三种,在Builder的OnDismissListener监听中将fragment移除掉。
附上关键代码:

FragmentManager fragmentManager = getSupportFragmentManager();
ImageFlowFragment mImageFlowFragment = (ImageFlowFragment) fragmentManager.findFragmentById(R.id.imagefragment);

/*
...
*/

.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.imagefragment)).commit();
                    }
                });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dialog_fragment_layout 是一个自定义的布局文件,用于显示弹窗的内容。该布局文件可以包含一个 FrameLayout 控件,用来承载需要展示的 Fragment。同时,可以在布局文件中添加上方的两个居中 TextView 以及底部的确定和取消按钮。 以下是一个示例的 dialog_fragment_layout 布局文件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@android:color/black" android:textAlignment="center"/> <TextView android:id="@+id/sub_title_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@android:color/black" android:textAlignment="center"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/confirm_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/> <Button android:id="@+id/cancel_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> </LinearLayout> </LinearLayout> ``` 在这个布局文件中,上方的两个居中 TextView 分别是 title_text_view 和 sub_title_text_view;底部的确定和取消按钮分别是 confirm_button 和 cancel_button;FrameLayout 控件是用来承载需要展示的 Fragment 的。注意,在实际使用中,需要根据具体的需求,对该布局文件进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值