博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
"ScrollView can host only one direct child"问题解决了
阅读量:6084 次
发布时间:2019-06-20

本文共 6766 字,大约阅读时间需要 22 分钟。

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=""
    xmlns: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=""
    xmlns: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>

 

 

版权声明:本文博客原创文章,博客,未经同意,不得转载。

你可能感兴趣的文章
iOS--OCR图片识别
查看>>
Handler和AsyncTask
查看>>
关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
查看>>
浅析微信支付:统一下单接口
查看>>
网络对抗技术_实验一_网络侦查与网络扫描
查看>>
黑板模式分析
查看>>
释放Win8.1 WinSxS冗余更新,微软Dism来解决
查看>>
【BZOJ】2243 [SDOI2011]染色
查看>>
springboot集成springsession利用redis来实现session共享
查看>>
文件上传与下载总结
查看>>
【测试基础】测试用例的设计方法
查看>>
MySQL优化-》执行计划和常见索引
查看>>
ThinkPHP中通过URL重写隐藏应用的入口文件index.php的相关服务器的配置
查看>>
18、图片 & 多媒体
查看>>
第七周进度总结
查看>>
Android任务栈的运行规律
查看>>
批处理通用测试代码
查看>>
uva 10594 Data Flow
查看>>
POJ 3592 Instantaneous Transference
查看>>
redis数据类型(字符串)
查看>>