最近在做一个小功能,即照片连拍的过程中,每拍一张弹出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();//显示对话框