본문 바로가기
Android/Exceptions

java.lang.IllegalStateException: ScrollView can host only one direct child

by featherwing 2020. 5. 3.
반응형

NestedScrollView과 ScrollView를 사용할 때 발생하였습니다.

 

xml레이아웃의 ScrollView안에 여러개의 Layout을 넣어두었을때 발생하였습니다.

 

 

scrollview.xml

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            
		<TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="text1"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/header_view"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/colorPrimary">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="text2"
                    android:textStyle="bold" />
            </LinearLayout>
</ScrollView>

 

스크롤뷰 내부에는 하나의 차일드 뷰만 들어가야 한다는 내용이며

 

아래와 같이 해당 xml에서 스크롤뷰 내부의 내용을 scrollview_inner.xml 의 새로운 레이아웃 xml파일로 만든 뒤

 

include로 넣어준 후에는 정상적으로 동작하였습니다.

 

 

scrollview.xml

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
       <include layout="@layout/scroll_view_inner"/>
       
</ScrollView>

 

 scrollview_inner.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
            
		<TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="text1"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/header_view"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="@color/colorPrimary">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="text2"
                    android:textStyle="bold" />
</LinearLayout>
반응형

댓글