Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
来源:互联网 发布:三流程序员写ui 编辑:程序博客网 时间:2024/06/15 16:02
今天实现了一个自定义对话框,当点击相应按钮时弹出该对话框。在测试过程中发现,第一次点击按钮时,对话框可以正常出现,但是在点击对话框上的“确定”或“取消”按钮回到主界面后,再次点击按钮,会报错如下:
E/AndroidRuntime(26211): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
出错处的代码为:
add.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {builder.setTitle("请输入新建相册名");builder.setView(linearLayout);builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {albumName = (EditText) linearLayout.findViewById(R.id.addname);String name = albumName.getText().toString();System.out.println("用户输入的相册名:" + name);}});builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {// TODO Auto-generated method stub}});builder.create().show(); }});在以上出错代码builder.setView(linearLayout)中,子视图是自定义的linearLayout,父视图是builder。而提示的大致意思是说为一个子视图指定了多个父视图。由此可以推断出,在第二次点击按钮弹出对话框时,子视图与第一次点击时的子视图是同一个对象,而父视图已经不再是同一个对象了。
查看代码发现,linearLayout对象是我在oncreate()方法中获得的:
linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.dialogname, null);所以在第二次点击按钮弹出对话框时,子视图还是之前那个在oncreate()方法中获取的、、
查明真相之后,只需在监听方法中加上获取linearLayout的语句即可:
linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.dialogname, null);builder.setTitle("请输入新建相册名"); builder.setView(linearLayout); ......之后测试,一切正常啦!!!
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- . 报错 specified child already has a parent. You must call removeView() on the child's parent first
- 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's pa
- Android自定义Dialog二次调用报错解决方法:The specified child already has a parent. You must call removeView()
- 自定义Dialog,第二次点击item报The specified child already has a parent. You must call removeView() on the chil
- 总是报The specified child already has a parent. You must call removeView() on the child's parent first.
- 报错:The specified child already has a parent. You must call removeView
- android The specified child already has a parent. You must call removeView() on the child's parent f
- The specified child already has a parent. You must call removeView() on the chil
- The specified child already has a parent. You must call removeView() on the
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- Android完美解决The specified child already has a parent. You must call removeView() on the child
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- Android AlertDialog嵌套及二调用报错解决方法:The specified child already has a parent. You must call removeView()
- Java文件Queue实现
- CFile Class
- ffmpeg 从mp4上提取H264的nalu
- 开发商可借手机游戏反哺传统游戏项目
- 了解面向对象技术
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Objective-c中的内存管理的黄金法则,OC中最重要的部分
- 解析手机游戏制胜10大关键因素
- windows 远程图形化控制 suse 与ubuntu
- 解密游戏社交新标准 微云官方发布WiGame 3.0
- 不同编程语言之间转换的项目矩阵
- 大学人生杂谈
- 20 本优秀的 Python 电子书
- Unity制作的应用在android平台上的发布步骤