java.lang.IllegalStateExceptionThe specified child already has a parent
来源:互联网 发布:sql server可视化界面 编辑:程序博客网 时间:2024/06/07 19:09
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
首先说下这个异常普遍出现在动态添加View时,出现。你第一次动态加载一个view到一个container时候不会报错,但是第二次动态加载到container时候会报这个异常。
我这里的问题这里的情形很像,我在一个AlertDialog中用setView方法加入一个editText控件,相当于在AlertDialog对话框中加入了一个控件,第一次弹出对话框的时候一切正常,但是我关闭了对话框,第二次弹出AlertDialog对话框时候就包这个错误。
由这个网址的结果说任何一个view组件只有一个parent组件,且view组件加入parent组件通过addView方法只能加一次。那么当我第二次创建AlertDialog组件并使用相同的setView方法把一个editView方法添加进去的时候,其实这个editText组件已经有parent组件了。所以会报错了。解决办法是在我们退出AlertDialog对话框时候的事件响应代码中将这个editText组件从AlertDialog中删除,使得editText组件成为一个独立的组件。
只要这么一句代码
if (editText.getParent() != null) { ((ViewGroup) editText.getParent()).removeAllViews(); }
package com.vincent.org.networkapp;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;/**这是一个自定义标题栏控件*/public class TitleLayout extends LinearLayout { protected static final String Tag = "TitleLayout"; private final EditText editText;//这个就是等下将插入到AlertDialog对话框中的editText控件 public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub editText = new EditText(context);//初始化 View view = LayoutInflater.from(context).inflate(R.layout.title_layout, this); //这里获取标题栏的两个按钮 Button back = (Button) view.findViewById(R.id.title_back); Button editbButton = (Button) view.findViewById(R.id.title_edit); final Dialog dialog = new Dialog(); //对按钮的单击事件触发响应代码 back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i(Tag, "back click on title"); ((Activity) getContext()).finish(); } }); editbButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i(Tag, "edit click on title"); new AlertDialog.Builder(getContext()).setTitle("title Edit") .setIcon(android.R.drawable.btn_dialog) .setView(editText).setPositiveButton("ok", dialog) .setNegativeButton("cancel", dialog).show(); } }); } //这里是对于AlertDialog对话框的两个ok和cancel按键的单击事件响应代码进行编写 class Dialog implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub switch (which) { case -1:// positive Log.i(Tag, "dialog positive button cancel" + editText.getText().toString()); //最重要的是在这里,将edittext这个控件从alertdialog对话框中删除。 if (editText.getParent() != null) { ((ViewGroup) editText.getParent()).removeAllViews(); } dialog.cancel(); break; case -2:// negative Log.i(Tag, "dialog negative button dismiss" + editText.getText().toString()); if (editText.getParent() != null) { ((ViewGroup) editText.getParent()).removeAllViews(); } dialog.dismiss(); break; case -3:// Neutral break; default: break; } } }}
参考文献
1
2
0 0
- java.lang.IllegalStateExceptionThe specified child already has a parent
- java.lang.IllegalStateException: The specified child already has a parent.
- java.lang.IllegalStateException:The specified child already has a parent.
- java.lang.IllegalStateException: The specified child already has a parent
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView
- Android异常分析之 java.lang.IllegalStateException:the specified child already has a parent.
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- Caused by: java.lang.IllegalStateException: The specified child already has a parent. You
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- Android—java.lang.IllegalStateException: The specified child already has a parent. You must call rem
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- Android | java.lang.IllegalStateException: The specified child already has a parent. 解决方案
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- 推荐好听歌曲下载
- Javascript Flash Access-Control-Allow-Origin 跨域
- RecyclerView
- iOS开发:UITableView性能优化
- gcc编译参数:如何包含头文件和动态链接库
- java.lang.IllegalStateExceptionThe specified child already has a parent
- leetcode29 Divide Two Integers
- [LeetCode] 144. Binary Tree Preorder Traversal
- C#调用webbrowser,阻止弹出新HTML页面
- Flume安装及简单部署
- Software Architecture Patterns
- 220. Contains Duplicate III
- 不想浪费时间写重复代码(二)
- 访问Google只需一步,超级简单