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

标签 java android xml android-layout android-ui

我只是试图通过添加 ScrollView 来添加滚动浏览此布局的功能,但是每次我尝试加载布局时都会收到一条错误消息,指出“java.lang.IllegalStateException: ScrollView can host only one direct child ”,我不确定为什么。

非常感谢任何建议。

来源:

<?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>

编辑(响应 CodeMagic 的回答)

<?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>

最佳答案

正如错误所说

ScrollView can host only one direct child

View 包装在 LinearLayout 中,这样 ScrollView 就只有 LinearLayout 作为直接子级.

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>

关于java.lang.IllegalStateException : ScrollView can host only one direct child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081217/

相关文章:

Android 操作栏操作项位置

java - 如何在java中使用DOM解析xml

android - CardView 默认背景色有问题

java - 使用 vaadin 7.3 和 valo 主题编译我的主题时出错

java - 无法读取文档 : Expected type float, 整数或字符串

java - 如何以字符串形式向 UI 显示 byte[]

android - Parse.com - Android 自定义推送通知声音

xml - 在 postgresql 上使用 sql 查询更新 xml 数据

c - libxml2 解析带有缩进的文档

java - 停止所有 Awt/Swing 线程和监视器以及其他东西,以便只剩下主线程