当前位置: 首页 > 面试题库 >

java.lang.IllegalStateException:ScrollView只能托管一个直接子级

丁宏浚
2023-03-14
问题内容

我只是试图通过添加滚动视图来增加在此布局中滚动的能力,但是每次尝试加载布局时,我都会收到一条错误消息,指出“
java.lang.IllegalStateException:ScrollView只能容纳一个直接子代”,并且我不确定为什么。

任何建议,不胜感激。

资源:

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

Views 包裹在a 的内部,LinearLayout因此ScrollView只有LinearLayoutas作为直接子代。

来自文档

ScrollView是一个FrameLayout,这意味着您应该在其中放置一个包含所有要滚动内容的子级;这个孩子本身可能是具有复杂对象层次结构的布局管理器。经常使用的子项是垂直方向的LinearLayout,它显示用户可以滚动浏览的顶级项目的垂直数组。

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


 类似资料:
  • 问题内容: 我是JSF的新手(2)。 在Struts中,我们可以从类似的URL调用动作。这将调用一个操作并返回一个结果页面,例如JSP(最初没有JSP / HTML)。 在JSF中如何做同样的事情?(我知道如何从.xhtml调用操作),即直接从URL调用托管bean并获取结果页面。 问题答案: 您要询问的模式并不是JSF工作方式的真正本机。 像Struts和Spring MVC这样的基于请求的框架

  • 我有一个托管bean,它有以下方法: 因此,方法将打开一个对话框,并传递给它两个参数(和)。 然后命令按钮将从调用方法: 但我在行中得到了一个NullPointerException: 所以我在这一行上附加了一个dubugger,我得到了这个: 您可以注意到,变量具有空值,而具有字符串值。

  • 本文向大家介绍html直接输入多个空格为什么只能显示一个空格?相关面试题,主要包含被问及html直接输入多个空格为什么只能显示一个空格?时的应答技巧和注意事项,需要的朋友参考一下 该行为由 CSS white-space 控制,其默认值 normal 的表现即为多个空格压缩成一个。

  • 问题内容: 从MySQL中的多个表导出数据的最佳方法是什么。我基本上是在处理产品详细信息。假设一个产品具有150个数据属性。如何将其导出为一行,然后将其导出为CSV或制表符分隔格式的平面文件。 错误越来越多 MySQL在一个联接中只能使用61个表 问题答案: 您正在使用EAV设计,并尝试根据可变数量的属性来重构单行。这指出了使用EAV设计将遇到的众多地雷之一:在单个SQL查询中可以执行的连接数量有

  • 问题内容: 我正在尝试使用jQuery_.on() 方法来理解 _直接 事件处理程序和 委托 事件处理程序之间的特殊区别。具体来说,本段的最后一句话: __ 当被提供时,事件处理程序被称为委托。当事件直接发生在绑定元素上时,不调用处理程序,而仅对与选择器匹配的后代(内部元素)进行调用。jQuery使事件从事件目标一直冒泡到附加了处理程序的元素(即,最内层元素到最外层元素),并沿该路径运行与选择器匹

  • 根据Android指南,http://developer.android.com/training/basics/fragments/communicating.html一个片段应该通过宿主Activity向另一个片段发送数据。我想知道这有什么原因。因为在我的代码中,我放置了一个变量来保存指向另一个片段的指针,并在onActivityCreated中赋值 后来,如果我想为FragmentType2