首页 > 解决方案 > .IllegalStateException: ScrollView 只能承载一个直接子级

问题描述

我非常努力地理解 ScrollView 和 Linearlayout 并不断尝试在我的活动中使用,但我遇到了这个错误。我在滚动视图中已经只有一个 Linearlyout 并且其他孩子被包裹在高度和宽度的包裹内容中但是应用程序仍然崩溃..请帮助

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff"
    tools:context=".Blood">

        <TextView
           android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:textStyle="bold"
            android:background="#f4051d"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Blood Type: Facts and Information"/>


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:padding="5dp">

<TextView
    android:id="@+id/intro"
   android:layout_width="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="#000000"
    android:fontFamily="sans-serif-condensed"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="When "/>


    <TextView
       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Types ( Groups )"/>

    <TextView
        android:id="@+id/blood_types"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="A blood "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image_one"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_one"/>

    <TextView
        android:id="@+id/blood_type_image"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="In addition ."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Donating Blood By Compatible Type"/>


    <TextView
        android:id="@+id/donate"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Blood "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <ImageView
        android:id="@+id/image_two"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_two"/>


    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Finding Out Your  Blood Type"/>


    <TextView
        android:id="@+id/find_blood_type"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="It is easy "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/image_three"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_blood_three"/>

    <TextView
        android:id="@+id/find_blood_image"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="In most"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="What Does Blood Group RH Factor Mean"/>


    <TextView
        android:id="@+id/rh_factor"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="RH factor ."
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />





    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="What is the Rarest Blood Type"/>


    <TextView
        android:id="@+id/rare_blood"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="According "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />



    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Type Diet"/>


    <TextView
        android:id="@+id/blood_type_diet"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text=" The Ea"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />




    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Blood Types and Personality"/>


    <TextView
        android:id="@+id/personality"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Legend"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    

    <TextView

       android:layout_width="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#0e06f2"
        android:fontFamily="sans-serif-condensed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Facts: Blood Types"/>


    <TextView
        android:id="@+id/facts"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:fontFamily="sans-serif-condensed"
        android:text="Blood"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    </ScrollView>
</LinearLayout>

提前致谢。

标签: javaandroidlayout

解决方案


你需要像这样在 ScrollView 中只有一个孩子。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView ... />
    <TextView ... />
    <TextView ... />
    <TextView ... />

</LinearLayout>
</ScrollView>

推荐阅读