"ScrollView can host only one direct child"問題解決

小鴻洋發表於2018-09-06

1. 問題描述:

ScrollView作為頂層view時報錯,直接導致apk崩潰,詳細錯誤資訊如下:

 ScrollView can host only one direct child

2. 分析:

從錯誤資訊ScrollView can host only one direct child
可見,是xml的錯誤,下面列出錯誤的xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/WhiteColor"
    android:scrollbars="vertical" >

   

        <!-- =================1. image ========================= -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:paddingTop="15dip" >

            <ImageView
                android:id="@+id/imageView_pic"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:contentDescription="text"
                android:scaleType="fitCenter"
                android:src="@drawable/empty_photo" />
        </LinearLayout>
       

        <!-- ========================= 2. detail ================= -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/text_prompt"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dp"
                android:gravity="center"
                android:text="@string/text_detail"
                android:textSize="15dip" />
        </LinearLayout>
        
        <!-- ========================= 3. line ================= -->
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="4dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="@color/lightgray" />
        </LinearLayout>


</ScrollView>

 

,這個ScrollView中包含3個LinerLayout。

3. 解決:

從錯誤資訊,我們就知道,需要scrollview不能有多個child,所以,我在3個layout外,又加了一個layout,使3個layou成為這個新加的layout的子view,問題解決。下面是正確的xml定義:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/WhiteColor"
    android:scrollbars="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="15dip" >

        <!-- =================1. image ========================= -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:paddingTop="15dip" >

            <ImageView
                android:id="@+id/imageView_pic"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:contentDescription="text"
                android:scaleType="fitCenter"
                android:src="@drawable/empty_photo" />
        </LinearLayout>
       

        <!-- ========================= 2. detail ================= -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/text_prompt"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dp"
                android:gravity="center"
                android:text="@string/text_detail"
                android:textSize="15dip" />
        </LinearLayout>
        
        <!-- ========================= 3. line ================= -->
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="32dp"
            android:layout_marginTop="4dp"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="@color/lightgray" />
        </LinearLayout>
</LinearLayout>

</ScrollView>

相關文章