Error:

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

原因:

这个问题的原因很简单,该异常表示已经存在父布局,不能再添加父布局。导致该异常的代码可能是:
View view = LayoutInflater.from(getContext()).inflate(R.layout.news_item, parent);

解决方法:

在 inflate() 方法上加入第三个参数,指定为 false 即可。

通常情况下,我们在获取View的时候一般代码都是:

View view = LayoutInflater.from(getContext()).inflate(R.layout.news_item, parent,false);

注意第三个参数为false,所以在获取View的时候并不会把parent设置为View的父布局。默认情况下,如果你不设置inflate的第三个参数,这个参数默认为parent!=null的值。
源码如下:

 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {        return inflate(resource, root, root != null);    } public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {        synchronized (mConstructorArgs) {            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");            final Context inflaterContext = mContext;            final AttributeSet attrs = Xml.asAttributeSet(parser);            Context lastContext = (Context) mConstructorArgs[0];            mConstructorArgs[0] = inflaterContext;            View result = root;            try {                // Look for the root node.                int type;                while ((type = parser.next()) != XmlPullParser.START_TAG &&                        type != XmlPullParser.END_DOCUMENT) {                    // Empty                }                if (type != XmlPullParser.START_TAG) {                    throw new InflateException(parser.getPositionDescription()                            + ": No start tag found!");                }                final String name = parser.getName();                if (DEBUG) {                    System.out.println("**************************");                    System.out.println("Creating root view: "                            + name);                    System.out.println("**************************");                }                if (TAG_MERGE.equals(name)) {                    if (root == null || !attachToRoot) {                        throw new InflateException(" can be used only with a valid "                                + "ViewGroup root and attachToRoot=true");                    }                    rInflate(parser, root, inflaterContext, attrs, false);                } else {                    // Temp is the root view that was found in the xml                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);                    ViewGroup.LayoutParams params = null;                    if (root != null) {                        if (DEBUG) {                            System.out.println("Creating params from root: " +                                    root);                        }                        // Create layout params that match root, if supplied                        params = root.generateLayoutParams(attrs);                        if (!attachToRoot) {                            // Set the layout params for temp if we are not                            // attaching. (If we are, we use addView, below)                            temp.setLayoutParams(params);                        }                    }                    if (DEBUG) {                        System.out.println("-----> start inflating children");                    }                    // Inflate all children under temp against its context.                    rInflateChildren(parser, temp, attrs, true);                    if (DEBUG) {                        System.out.println("-----> done inflating children");                    }                    // We are supposed to attach all the views we found (int temp)                    // to root. Do that now.                    if (root != null && attachToRoot) {                        root.addView(temp, params);                    }                    // Decide whether to return the root that was passed in or the                    // top view found in xml.                    if (root == null || !attachToRoot) {                        result = temp;                    }                }            } catch (XmlPullParserException e) {                final InflateException ie = new InflateException(e.getMessage(), e);                ie.setStackTrace(EMPTY_STACK_TRACE);                throw ie;            } catch (Exception e) {                final InflateException ie = new InflateException(parser.getPositionDescription()                        + ": " + e.getMessage(), e);                ie.setStackTrace(EMPTY_STACK_TRACE);                throw ie;            } finally {                // Don't retain static reference on context.                mConstructorArgs[0] = lastContext;                mConstructorArgs[1] = null;                Trace.traceEnd(Trace.TRACE_TAG_VIEW);            }            return result;        }    }

相信读完源码,大家 就会对这个异常晃人大悟了。

更多相关文章

  1. 如何在Android平台上使用JS直接调用Java方法
  2. android 打开Eclipse之后android项目都报错怎么解决
  3. Android(安卓)短信发送
  4. Android(安卓)软键盘弹出时把布局顶上去,控件乱套解决方法
  5. Android(安卓)Studio 提示错误 default activity not found
  6. Android实现图片叠加效果的两种方法
  7. android――activity添加退出动画
  8. android中expandablelistview的学习
  9. android 访问并显示网页

随机推荐

  1. Android之布局
  2. Android系统自带样式(@android:style/)
  3. Android(安卓)title 高度定义
  4. android 改变输入法enter键文字 为搜索
  5. 关于Android中的 ToggleButton 组件 (待续
  6. Android(安卓)自定义dialog
  7. 【Android】代码中设置drawableLeft|Top|
  8. Android日历控件
  9. Linux Android(安卓)cocos2dx环境变量设
  10. Android(安卓)Studio中Gradle使用详解