宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 个人博客 > android

You must call removeView() on the child's parent first

2016-10-13 00:16:48 点击量:138 标签: 收藏本文

在做alertdialog是的时候报了这么一个错误:

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

搞了许久,终于理解了。

et1 = (EditText)findViewById(R.id.editText1);
builder.setView(et1);  -- AlertDialog.Builder builder

et1我写在了xml里面,这样报错,原因是一女不可二嫁。

et1的parent即是R.layout.main 又是AlertDialog。

自然就报错了要你removeView()了。

解决方法有两种

1.动态生成EditText

et1 = new EditText(this);
builder.setView(et1);

2. 放在另一个xml中,用inflater

LayoutInflater inflater = LayoutInflater.from(this);  
            View textEntryView = inflater.inflate(R.layout.test1, null);  
            et1 = (EditText)textEntryView.findViewById(R.id.editText1);
builder.setView(textEntryView ); 注意这里是textEntryView ,不是et1 
ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室