{"record":{"id":"02cf4d0c8b3baba3","repo":"DrKLO/Telegram","slug":"view-child-is-not-a-direct-child-of-this","errorCode":null,"errorMessage":"View {child} is not a direct child of {this}","messagePattern":"View (.+?) is not a direct child of (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java","lineNumber":4784,"sourceCode":"     * @param preserveFocusAfterLayout Whether RecyclerView should preserve focused Item during a\n     *                                 layout calculations. Defaults to true.\n     *\n     * @see #getPreserveFocusAfterLayout()\n     */\n    public void setPreserveFocusAfterLayout(boolean preserveFocusAfterLayout) {\n        mPreserveFocusAfterLayout = preserveFocusAfterLayout;\n    }\n\n    /**\n     * Retrieve the {@link ViewHolder} for the given child view.\n     *\n     * @param child Child of this RecyclerView to query for its ViewHolder\n     * @return The child view's ViewHolder\n     */\n    public ViewHolder getChildViewHolder(@NonNull View child) {\n        final ViewParent parent = child.getParent();\n        if (parent != null && parent != this) {\n            throw new IllegalArgumentException(\"View \" + child + \" is not a direct child of \"\n                    + this);\n        }\n        return getChildViewHolderInt(child);\n    }\n\n    /**\n     * Traverses the ancestors of the given view and returns the item view that contains it and\n     * also a direct child of the RecyclerView. This returned view can be used to get the\n     * ViewHolder by calling {@link #getChildViewHolder(View)}.\n     *\n     * @param view The view that is a descendant of the RecyclerView.\n     *\n     * @return The direct child of the RecyclerView which contains the given view or null if the\n     * provided view is not a descendant of this RecyclerView.\n     *\n     * @see #getChildViewHolder(View)\n     * @see #findContainingViewHolder(View)\n     */","sourceCodeStart":4766,"sourceCodeEnd":4802,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java#L4766-L4802","documentation":"RecyclerView.getChildViewHolder(@NonNull View child) throws IllegalArgumentException when the supplied view's parent is non-null and is not the RecyclerView itself. The method only resolves a ViewHolder for a DIRECT child view; passing a descendant (e.g. a button nested inside an item's root view) or a view belonging to a different parent violates the precondition. Call getChildViewHolderInt for internal resolution; this public API enforces direct-childhood to avoid returning a ViewHolder for the wrong RecyclerView.","triggerScenarios":"Passing a descendant of an item view (e.g. itemView.findViewById(...)) instead of the item root; passing a view that belongs to a different RecyclerView or was already removed from the hierarchy; calling getChildViewHolder during a transient state where the view is detached.","commonSituations":"Click handlers that pass the clicked inner view (a TextView/Button) rather than the outer card/item root; migrating from ListView where getChildAt was used loosely; views recycled and rebound between queries.","solutions":["Pass the direct item root view: in a click listener use View itemView = (View) v.getParent() up to the RecyclerView child, or use vh.itemView; better, capture the ViewHolder in the listener when binding rather than re-querying later.","Use RecyclerView.getChildAdapterPosition(view) with the item root, or set an OnClickListener on the ViewHolder.itemView during onBindViewHolder.","Before calling, check view.getParent() == recyclerView to guard against detached/migrated views."],"exampleFix":"// before: passing the clicked inner view\nitemView.findViewById(R.id.btn).setOnClickListener(v -> {\n  ViewHolder vh = (ViewHolder) rv.getChildViewHolder(v); // v is not a direct child -> throw\n});\n// after: capture the holder at bind time\nholder.itemView.setOnClickListener(v -> {\n  int pos = holder.getAdapterPosition();\n  ... // no lookup needed\n});","handlingStrategy":"validation","validationCode":"ViewParent p = view.getParent();\nif (p == recyclerView) {\n  RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(view);\n} else {\n  // walk up to the direct child, or restructure to capture the holder at bind time\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Capture the ViewHolder/position in the click listener during onBindViewHolder rather than re-querying later.","Only pass the item root view (holder.itemView) to getChildViewHolder.","Check view.getParent() == recyclerView before calling."],"tags":["recyclerview","viewholder","view-hierarchy","api-misuse"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}