{"record":{"id":"24fb9274394123b4","repo":"unoplatform/uno","slug":"unotwodscrollview-can-host-only-one-direct-child","errorCode":null,"errorMessage":"UnoTwoDScrollView can host only one direct child","messagePattern":"UnoTwoDScrollView can host only one direct child","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/Uno.UI.BindingHelper.Android/Uno/UI/UnoTwoDScrollView.java","lineNumber":235,"sourceCode":"\tprivate void initTwoDScrollView() {\n\t\tmScroller = new OverScroller(getContext());\n\t\tsetFocusable(true);\n\t\tsetDescendantFocusability(FOCUS_AFTER_DESCENDANTS);\n\t\tsetWillNotDraw(false);\n\t\tfinal ViewConfiguration configuration = ViewConfiguration.get(getContext());\n\t\tmTouchSlop = configuration.getScaledTouchSlop();\n\t\tmMinimumVelocity = configuration.getScaledMinimumFlingVelocity();\n\t\tmMaximumVelocity = configuration.getScaledMaximumFlingVelocity();\n\t\tmOverscrollDistance = configuration.getScaledOverscrollDistance();\n\t\tmOverflingDistance = configuration.getScaledOverflingDistance();\n\n\t\tmscaleGestureDetector = new UnoScrollViewScaleGestureDetector(getContext(), new UnoScrollViewScaleGestureDetector.UnoScaleGestureListener(this));\n\t}\n\n\t@Override\n\tpublic void addView(View child) {\n\t\tif (getChildCount() > 0) {\n\t\t\tthrow new IllegalStateException(\"UnoTwoDScrollView can host only one direct child\");\n\t\t}\n\t\tsuper.addView(child);\n\t}\n\n\t@Override\n\tpublic void addView(View child, int index) {\n\t\tif (getChildCount() > 0) {\n\t\t\tthrow new IllegalStateException(\"UnoTwoDScrollView can host only one direct child\");\n\t\t}\n\t\tsuper.addView(child, index);\n\t}\n\n\t@Override\n\tpublic void addView(View child, ViewGroup.LayoutParams params) {\n\t\tif (getChildCount() > 0) {\n\t\t\tthrow new IllegalStateException(\"UnoTwoDScrollView can host only one direct child\");\n\t\t}\n\t\tsuper.addView(child, params);","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/Uno.UI.BindingHelper.Android/Uno/UI/UnoTwoDScrollView.java#L217-L253","documentation":"Thrown by UnoTwoDScrollView.addView(View child) when getChildCount() is already greater than zero. UnoTwoDScrollView (like Android's ScrollView/HorizontalScrollView) enforces a single direct child — a container that wraps the scrollable content. Adding a second direct child violates this invariant.","triggerScenarios":"XAML or code adds more than one child directly to the UnoTwoDScrollView instead of wrapping multiple children in a single container (e.g. a StackPanel/Grid).","commonSituations":"A XAML layout places two sibling elements directly inside the ScrollView content without a wrapping panel; dynamically calling AddView on the scroll host without checking existing children.","solutions":["Wrap all scroll content in a single container (Grid, StackPanel, etc.) and set that container as the only child of the ScrollView.","If adding children dynamically, always add to the inner content panel, not the ScrollView itself."],"exampleFix":"// before: two direct children\nscrollView.addView(childA);\nscrollView.addView(childB); // throws\n\n// after: single wrapper child\nvar wrapper = new LinearLayout(context);\nwrapper.addView(childA);\nwrapper.addView(childB);\nscrollView.addView(wrapper); // single child — OK","handlingStrategy":"validation","validationCode":"// Before addView, verify no child exists\nif (scrollView.getChildCount() === 0) {\n    scrollView.addView(child);\n} else {\n    // add to the existing single content child instead\n}","typeGuard":null,"tryCatchPattern":"try {\n    scrollView.addView(child);\n} catch (e) {\n    if (e.getMessage().contains('only one direct child')) {\n        // redirect to inner content panel\n    } else { throw e; }\n}","preventionTips":["Always wrap multiple children in a single container before adding to a ScrollView.","In XAML, ensure the ScrollView has exactly one child element.","Target dynamic child additions at the content panel, not the scroll host."],"tags":["android","scrollview","layout","java","view-group"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}