使用LinearLayout中以编程方式添加的视图的RecyclerView ViewHolder会出现错误"您必须首先在孩子的父母身上调用removeView()"

时间:2016-02-09 19:44:51

标签: android android-layout android-recyclerview android-viewholder

我正在RecyclerView.ViewHolder中使用RecyclerView。在这个视图中,我有一个LinearLayout我以编程方式向容器添加视图。

当我运行它时,我收到此错误:

02-09 19:15:57.532 23106-23106/com.fusionprojects.edmodo E/AndroidRuntime: FATAL EXCEPTION:main

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
at android.view.ViewGroup.addView(ViewGroup.java:3210)
at android.view.ViewGroup.addView(ViewGroup.java:3155)
at android.view.ViewGroup.addView(ViewGroup.java:3131)
at com.edmodo.stream.PostAttachmentsViewHolder.showFileAttachments(PostAttachmentsViewHolder.java:117)
at com.edmodo.stream.PostAttachmentsViewHolder.setAttachments(PostAttachmentsViewHolder.java:75)
at com.edmodo.stream.StreamPostViewHolder.setPost(StreamPostViewHolder.java:47)
at com.edmodo.postsstream.PostStreamAdapter.onBindViewHolder(PostStreamAdapter.java:49)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:543)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14003)
at android.view.ViewGroup.layout(ViewGroup.java:4375)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)                       
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.

我尝试调用removeAllViews()并循环浏览LinearLayout个孩子并单独删除,但我仍然遇到同样的错误。

如何处理此问题并正确删除LinearLayout容器中的所有视图?

2 个答案:

答案 0 :(得分:0)

被添加到LinearLayout的孩子有父母。 在添加新视图之前,请尝试以下操作。

View view = ... // The View you're adding to the LinearLayout
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
    parent.removeView(view);
}
// Proceed to add the view on your LinearLayout.

虽然可能存在潜在问题,但这可能是一种解决方法。

答案 1 :(得分:0)

绑定对象时,在ViewHolder类中调用linearLayout.removeAllViews()。