---
title: "The specified child already has a parent. You must call removeView() on the chil"
author: "Darar"
date: "2012-06-27T17:23:33Z"
tags: []
categories: ["移动开发"]
slug: "the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil"
draft: false
lastmod: "2012-06-27T17:23:33Z"
id: 1569235
url: "https://darar.iteye.com/blog/1569235"
---

在开发中这么一个场景。如果你是用onCreateDialog showDialog 系统的一系列方法去操作dialog 那么当你dialog存在时，你离开了当前的activity。再回到此activity时，点击dialog上的按钮，按钮的事件是显示另一个dialog。则你会发现以下异常:

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

 

原因

1\. 在你离开界面再回来时，系统会掉Activity中的performRestoreInstanceState方法。此方法里把所有的dialog又重新初始化了一遍。则当你触发showDialog操作时， 就会出现以上异常，说你必须先removeView才能再addView.。所以当你的界面需要在离开页面 dialog不关的场景下， 最好不要用系统的dialog操作方法。