This is an example of the code that I followed.
And the code below is part of the onCreateViewHolder in the sample code:
@Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); // set the view size, margins, paddings and layout parameters ... ViewHolder vh = new ViewHolder(v); return vh; }
And this is part of ViewHolder:
public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } }
Obviously, the TextView v parameter is incorrect.
Then I play the trick in the onCreateViewHolder method as follows:
... TextView textView = (TextView) v.findViewById(R.id.tv_test); ViewHolder vh = new ViewHolder(textView);
But when I started the application, I got an exception.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:3693) at android.view.ViewGroup.addView(ViewGroup.java:3546) at android.view.ViewGroup.addView(ViewGroup.java:3491) at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:3533) at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:3558) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1012) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:524) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600) at android.view.View.layout(View.java:15273) at android.view.ViewGroup.layout(ViewGroup.java:4763) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1069) at android.view.View.layout(View.java:15273) at android.view.ViewGroup.layout(ViewGroup.java:4763) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457) at android.widget.FrameLayout.onLayout(FrameLayout.java:392) at android.view.View.layout(View.java:15273) at android.view.ViewGroup.layout(ViewGroup.java:4763) at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494) at android.view.View.layout(View.java:15273) at android.view.ViewGroup.layout(ViewGroup.java:4763) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:457) at android.widget.FrameLayout.onLayout(FrameLayout.java:392) at android.view.View.layout(View.java:15273) at android.view.ViewGroup.layout(ViewGroup.java:4763) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2057) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1814) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1044) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5749) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:738) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5070) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
BUT, when I change the ViewHolder code as follows:
public ViewHolder(View v) { super(v); mTextView = (TextView) v; }
Everything will be good.
I want to know what this exception means? What happened to this exception?
source share