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

來源:互聯網
上載者:User

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

1. 問題描述:

(注意下面的黑體字)。

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

12-21 09:12:15.150: D/AndroidRuntime(7726): Shutting down VM
12-21 09:12:15.150: W/dalvikvm(7726): threadid=1: thread exiting with uncaught exception (group=0x416402a0)
12-21 09:12:15.160: E/AndroidRuntime(7726): FATAL EXCEPTION: main
12-21 09:12:15.160: E/AndroidRuntime(7726): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.fish.app/com.my.act.activity.my.MyFA}: java.lang.IllegalStateException: ScrollView can host only one direct child
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.os.Looper.loop(Looper.java:137)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread.main(ActivityThread.java:4921)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at java.lang.reflect.Method.invokeNative(Native Method)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at java.lang.reflect.Method.invoke(Method.java:511)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at dalvik.system.NativeStart.main(Native Method)
12-21 09:12:15.160: E/AndroidRuntime(7726): Caused by: java.lang.IllegalStateException:ScrollView can host only one direct child
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.widget.ScrollView.addView(ScrollView.java:316)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:307)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.Activity.setContentView(Activity.java:1924)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.my.act.InitTitlebar(MyFA.java:113)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.ui.activity.base.BaseFA.onCreate(BaseFA.java:29)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at com.fish.ui.activity.my.MyFA.onCreate(MyFA.java:169)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.Activity.performCreate(Activity.java:5206)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-21 09:12:15.160: E/AndroidRuntime(7726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-21 09:12:15.160: E/AndroidRuntime(7726):  ... 11 more

 

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>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.