Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm simply trying to add the ability to scroll through this layout by adding a scrollview however every time I attempt to load the layout I'm getting an error stating "java.lang.IllegalStateException: ScrollView can host only one direct child" and I'm unsure why.

Any suggestions are much appreciated.

SOURCE:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical" >

    <RelativeLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="5dp" >
        </View>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayerview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="1dp"
                android:layout_height="5dp" >
            </View>

            <TextView
                android:id="@+id/textView1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Isaac Daniel at CNN Anderson Cooper"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="by idconex"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textView3a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="675,000,000 views"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayerview2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rage Against The Machine - Bulls on Parade"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="by RATMVEVO"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textView3b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1,195,601 views"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </ScrollView>
    </RelativeLayout>

</LinearLayout>

EDIT (In response to CodeMagic's answer)

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical" >


<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
      <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content">



            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayerview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <View
                android:layout_width="1dp"
                android:layout_height="5dp" >
            </View>

            <TextView
                android:id="@+id/textView1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Isaac Daniel at CNN Anderson Cooper"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="by idconex"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textView3a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="675,000,000 views"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubeplayerview2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rage Against The Machine - Bulls on Parade"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="by RATMVEVO"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                android:id="@+id/textView3b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1,195,601 views"
                android:textAppearance="?android:attr/textAppearanceSmall" />

         </LinearLayout>
like image 812
user3009687 Avatar asked Nov 19 '13 20:11

user3009687


1 Answers

Just as the error says

ScrollView can host only one direct child

Wrap the Views inside of a LinearLayout so the ScrollView has only the LinearLayout as a direct child.

From the Docs

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
      <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
           // all the views currently in your ScrollView
     </LinearLayout>
</ScrollView>
like image 77
codeMagic Avatar answered Sep 23 '22 19:09

codeMagic