带EditText的 AlertDialog 第二次点击报错,child already has a parent错误的解决

本文解决了一个Android应用中,在连续触发Dialog操作时遇到的IllegalStateException错误。通过调整Dialog的创建和显示时机,避免了子视图与父视图状态不匹配的问题,从而实现了应用的稳定运行。
摘要由CSDN通过智能技术生成

       最近在做一个小功能,即照片连拍的过程中,每拍一张弹出AlertDialog输入添加文本后,画成水印在照片上。过程中遇到了一个问题,即第一张照片添加完毕后,进入第二张拍照,点击拍照返回即报错,是AlertDialog的问题,log如下:

07-23 11:00:32.480: E/AndroidRuntime(15932): FATAL EXCEPTION: main
07-23 11:00:32.480: E/AndroidRuntime(15932): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=null} to activity {com.yds.sad.activity/com.yds.sad.activity.BaseStationActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3440)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:3490)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread.access$1100(ActivityThread.java:139)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.os.Looper.loop(Looper.java:156)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread.main(ActivityThread.java:5060)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at java.lang.reflect.Method.invoke(Method.java:511)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at dalvik.system.NativeStart.main(Native Method)
07-23 11:00:32.480: E/AndroidRuntime(15932): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.view.ViewGroup.addViewInner(ViewGroup.java:3654)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.view.ViewGroup.addView(ViewGroup.java:3525)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.view.ViewGroup.addView(ViewGroup.java:3505)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at com.android.internal.app.AlertController.setupView(AlertController.java:426)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at com.android.internal.app.AlertController.installContent(AlertController.java:249)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.AlertDialog.onCreate(AlertDialog.java:336)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.Dialog.dispatchOnCreate(Dialog.java:384)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.Dialog.show(Dialog.java:280)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at com.yds.sad.fragment.BaseStation_PhotoFragment.onActivityResult(BaseStation_PhotoFragment.java:172)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.Activity.dispatchActivityResult(Activity.java:4752)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3436)
07-23 11:00:32.480: E/AndroidRuntime(15932): 	... 11 more

错误为:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

在第二次点击按钮弹出对话框时,子视图与第一次点击时的子视图是同一个对象,而父视图已经不再是同一个对象了。

其实是个很小的问题,之前inflate代码放的位置不对。AlertDialog的setView(),每次应该在对话框setview之前,新建子view,重新inflate子xml布局。代码修改后如下运行正常:

ad=new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
rl = (RelativeLayout) inflater.inflate(R.layout.basestation_photo_waterdialog, null);
mEtWaterMarker = (EditText)rl.findViewById(R.id.et_basestation_photo_wateret);
ad.setTitle("添加水印");
ad.setView(rl);
ad.setCancelable(false);
ad.setIcon(android.R.drawable.ic_dialog_alert);
ad.create();
ad.show();//显示对话框


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值