java.lang.IllegalStateException:滚动型只能承载一个直接子 [英] java.lang.IllegalStateException: ScrollView can host only one direct child

查看:218
本文介绍了java.lang.IllegalStateException:滚动型只能承载一个直接子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图但是每次我尝试加载的布局,我得到一个错误,指出java.lang.IllegalStateException增加一个滚动视图添加到滚动此布局的能力:滚动型只能承载一个直接孩子,我不确定为什么。

任何建议都多少AP preciated。

来源:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =1
    机器人:方向=垂直>

    < RelativeLayout的>

        <查看
            机器人:layout_width =1DP
            机器人:layout_height =5DP>
        < /视图>

        <滚动型
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1>

            < com.google.android.youtube.player.YouTubePlayerView
                机器人:ID =@ + ID / youtubeplayerview
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>

            <查看
                机器人:layout_width =1DP
                机器人:layout_height =5DP>
            < /视图>

            <的TextView
                机器人:ID =@ + ID / textView1a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=艾萨克丹尼尔在CNN的安德森·库珀
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

            <的TextView
                机器人:ID =@ + ID / textView2a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=由idconex
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            <的TextView
                机器人:ID =@ + ID / textView3a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=6.75亿的意见
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            < com.google.android.youtube.player.YouTubePlayerView
                机器人:ID =@ + ID / youtubeplayerview2
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>

            <的TextView
                机器人:ID =@ + ID / textView1b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=愤怒反对机器 - 巡游公牛
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

            <的TextView
                机器人:ID =@ + ID / textView2b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=由RATMVEVO
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            <的TextView
                机器人:ID =@ + ID / textView3b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=1195601意见
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?
        < /滚动型>
    < / RelativeLayout的>

< / LinearLayout中>
 

编辑(响应codeMagic的答案)

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =1
    机器人:方向=垂直>


<滚动型
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1>
      < LinearLayout中的android:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT>



            < com.google.android.youtube.player.YouTubePlayerView
                机器人:ID =@ + ID / youtubeplayerview
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>

            <查看
                机器人:layout_width =1DP
                机器人:layout_height =5DP>
            < /视图>

            <的TextView
                机器人:ID =@ + ID / textView1a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=艾萨克丹尼尔在CNN的安德森·库珀
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

            <的TextView
                机器人:ID =@ + ID / textView2a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=由idconex
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            <的TextView
                机器人:ID =@ + ID / textView3a
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=6.75亿的意见
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            < com.google.android.youtube.player.YouTubePlayerView
                机器人:ID =@ + ID / youtubeplayerview2
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT/>

            <的TextView
                机器人:ID =@ + ID / textView1b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=愤怒反对机器 - 巡游公牛
                机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

            <的TextView
                机器人:ID =@ + ID / textView2b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=由RATMVEVO
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

            <的TextView
                机器人:ID =@ + ID / textView3b
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=1195601意见
                机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceSmall?

         < / LinearLayout中>
 

解决方案

正如错误说:

 滚动型只能承载一个直接子
 

裹在查看 S内部的的LinearLayout ,以便滚动型只具有的LinearLayout 作为直接孩子。

从文档

  

一个滚动型是一个的FrameLayout,这意味着你应该把一个孩子它包含的全部内容,以滚动;这个孩子本身可能是使用对象的层次复杂的布局管理器。经常被使用的儿童是一个的LinearLayout在垂直方向上,presenting的顶级项目一个垂直阵列,该用户可以滚动通过

 <滚动型
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1>
      < LinearLayout中的android:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:方向=垂直>
           //在你的滚动型的当前看法
     < / LinearLayout中>
< /滚动型>
 

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>

解决方案

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>

这篇关于java.lang.IllegalStateException:滚动型只能承载一个直接子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆