Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
2012-12-30 20:29
597 查看
今天实现了一个自定义对话框,当点击相应按钮时弹出该对话框。在测试过程中发现,第一次点击按钮时,对话框可以正常出现,但是在点击对话框上的“确定”或“取消”按钮回到主界面后,再次点击按钮,会报错如下:
E/AndroidRuntime(26211): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
出错处的代码为:
查看代码发现,linearLayout对象是我在oncreate()方法中获得的:
查明真相之后,只需在监听方法中加上获取linearLayout的语句即可:
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 child's paren
- Fragment使用时出现错误:he specified child already has a parent. You must call removeView() on the child's p
- 【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 parent f
- 对话框子视图Android自定义Dialog二次调用报错解决方法:The specified child already has a parent. You must call removeView()-java教程
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- 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() on the child's parent first.
- The specified child already has a parent. You must call removeView() on the child's parent first问题
- viewpage:The specified child already has a parent. " + "You must call removeView() on the child's pa
- 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() on the chil
- . 报错 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() on the child's parent first.
- [已]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() on the child's parent first.问解决
- 【Android异常】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()问题的解决